Dictionaries and dot notation

Daniel Nogradi nogradi at gmail.com
Sun Apr 22 16:28:23 CEST 2007


> > This may be pretty obvious for most of you:
> >
> > When I have an object (an instance of a class "Foo") I can access
> > attributes via dot notation:
> >
> >         aFoo.bar
> >
> > however when I have a dictionary
> >
> >         aDict = {"bar":"something"}
> >
> > I have to write
> >
> >         aDict["bar"]
> >
> > What if I want to create a datastructure that can be used in dot
> > notation without having to create a class, i.e. because those objects
> > have no behavior at all?
> >
> > I know that accessing an instance variable via bracket notation would
> > really have to be written as:
> >
> >         aFoo.__dict__['bar']
> >
> > but this does not bring me any further, because I would still have to
> > plug in that __dict__ thing into my datastructure, which leads us to
> > the same question as above.
> >
> > Can anyone tell me what I am missing here?


What's wrong with creating a dummy class?

class data:
    pass

mydata = data( )
mydata.foo = 'foo'
mydata.bar = 'bar'

print mydata.foo
print mydata.bar



Daniel



More information about the Python-list mailing list