Dictionaries and dot notation
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sun Apr 22 12:30:32 EDT 2007
Martin Drautzburg a écrit :
> 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?
A class inheriting from dict and implementing __getattr__ and
__setattr__ should do the trick...
More information about the Python-list
mailing list