Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to treat Python argparse.Namespace() as a dictionary?

If I want to use the results of argparse.ArgumentParser(), which is a Namespace object, with a method that expects a dictionary or mapping-like object (see collections.Mapping), what is the right way to do it?

C:\>python Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import argparse >>> args = argparse.Namespace() >>> args.foo = 1 >>> args.bar = [1,2,3] >>> args.baz = 'yippee' >>> args['baz'] Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: 'Namespace' object has no attribute '__getitem__' >>> dir(args) ['__class__', '__contains__', '__delattr__', '__dict__', '__doc__', '__eq__', '_ _format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__ ', '__str__', '__subclasshook__', '__weakref__', '_get_args', '_get_kwargs', 'ba r', 'baz', 'foo'] 

Is it proper to "reach into" an object and use its __dict__ property?

I would think the answer is no: __dict__ smells like a convention for implementation, but not for an interface, the way __getattribute__ or __setattr__ or __contains__ seem to be.

like image 709
Jason S Avatar asked Jun 01 '13 23:06

Jason S


People also ask

How do you access the namespace object in Python?

To get access to local namespace dict you can call locals() or if you want to access any object's namespace call vars(objname) . Inside function if you call locals() or vars() you will get currently visible namespace as dictionary and should not be modified.

How do you create a namespace in Python?

So if you want to create a namespace, you just need to call a function, instantiate an object, import a module or import a package. For example, we can create a class called Namespace and when you create an object of that class, you're basically creating a namespace.

How does Argparse work in Python?

The standard Python library argparse used to incorporate the parsing of command line arguments. Instead of having to manually set variables inside of the code, argparse can be used to add flexibility and reusability to your code by allowing user input values to be parsed and utilized.

What is namespace object Python?

Namespaces in Python. A namespace is a collection of currently defined symbolic names along with information about the object that each name references. You can think of a namespace as a dictionary in which the keys are the object names and the values are the objects themselves.


1 Answers

You can access the namespace's dictionary with vars():

>>> import argparse >>> args = argparse.Namespace() >>> args.foo = 1 >>> args.bar = [1,2,3] >>> d = vars(args) >>> d {'foo': 1, 'bar': [1, 2, 3]} 

You can modify the dictionary directly if you wish:

>>> d['baz'] = 'store me' >>> args.baz 'store me' 

Yes, it is okay to access the __dict__ attribute. It is a well-defined, tested, and guaranteed behavior.

like image 109
Raymond Hettinger Avatar answered Sep 21 '22 05:09

Raymond Hettinger