Dictionaries and dot notation

Stefan Behnel stefan.behnel-n05pAM at web.de
Sun Apr 22 09:24:55 EDT 2007


Martin Drautzburg wrote:
> 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?

This?

http://docs.python.org/ref/attribute-access.html

Stefan



More information about the Python-list mailing list