Dictionaries and dot notation

Alex Martelli aleax at mac.com
Sun Apr 22 23:27:15 EDT 2007


Martin Drautzburg <Martin.Drautzburg at web.de> wrote:

> > mydata = data( )
> > mydata.foo = 'foo'
> > mydata.bar = 'bar'
> > 
> > print mydata.foo
> > print mydata.bar
> 
> I am aware of all this.
> Okay let me rephrase my question: is there a way of using dot notation
> without having to create a class?

Sure, all you need to create is an *INSTANCE* of a suitable type or
class.  For example:

>>> d = dict(foo=23, bar=45)
>>> m = new.module('for_martin')
>>> m.__dict__.update(d)
>>> m.foo
23
>>> m.bar
45
>>> 

A module may be appropriate, since it's little more than a "wrapper
around a dict to access items by dot notation":-).


Alex



More information about the Python-list mailing list