Dictionaries and dot notation
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sun Apr 22 12:32:48 EDT 2007
Bruno Desthuilliers a écrit :
> 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...
Oh, yes, if you don't care about dict-like behaviour, you can also just:
class Data(object):
def __init__(self, **kw):
self.__dict__.update(kw)
More information about the Python-list
mailing list