namespace dictionaries ok?

Ron Adam rrr at ronadam.com
Mon Oct 24 22:06:34 EDT 2005


Hi, I found the following to be a useful way to access arguments after 
they are passed to a function that collects them with **kwds.


     class namespace(dict):
         def __getattr__(self, name):
             return self.__getitem__(name)
         def __setattr__(self, name, value):
             self.__setitem__(name, value)
         def __delattr__(self, name):
             self.__delitem__(name)

     def foo(**kwds):
	kwds = namespace(kwds)
	print kwds.color, kwds.size, kwds.shape  etc....

     foo( color='red', size='large', shape='ball', .... etc..)


It just seems awkward to have to use "string keys" in this situation. 
This is easy and still retains the dictionary so it can be modified and 
passed to another function or method as kwds again.

Any thoughts?  Any better way to do this?

Cheers, Ron




More information about the Python-list mailing list