What is a namespace object?

Shashank Garg picture Shashank Garg · Dec 29, 2013 · Viewed 27.3k times · Source
import argparse

parser = argparse.ArgumentParser(description='sort given numbers')
parser.add_argument('-s', nargs = '+', type = int)
args = parser.parse_args()
print(args)

On command line when I run the command

python3 file_name.py -s 9 8 76

It prints Namespace(s=[9, 8, 76]).

How can I access the list [9, 8, 76]? What is the namespace object. Where can I learn more about it?

Answer

Bill Lynch picture Bill Lynch · Dec 29, 2013
  • The documentation for argparse.Namespace can be found here.
  • You can access the s attribute by doing args.s.
  • If you'd like to access this as a dictionary, you can do vars(args), which means you can also do vars(args)['s']