empty classes as c structs?

Steven Bethard steven.bethard at gmail.com
Sat Feb 5 14:07:25 EST 2005


Nick Coghlan wrote:
> Alex Martelli wrote:
> 
>> Nick Coghlan <ncoghlan at iinet.net.au> wrote:
>>    ...
>>
>>> Michael Spencer also posted an interesting idea recently about 
>>> setting up
>>> a view of an existing dictionary, rather than as a separate object:
>>>
>>> class attr_view(object):
>>>   def __init__(self, data):
>>>     object.__setattr__(self, "_data", data)
>>>   def __getattr__(self, attrname):
>>>     return self._data[attrname]
>>>   def __setattr__(self, attrname, value):
>>>     self._data[attrname] = value
>>
>> Wasted indirection, IMHO.  A better implementation:
>>
>> class attr_view(object):
>>     def __init__(self, data):
>>         self.__dict__ = data
> 
> I think the idea definitely deserves mention as a possible 
> implementation strategy in the generic objects PEP, with the data 
> argument made optional:

That's basically what the current implementation does (although I use 
'update' instead of '=').  The code is complicated because the 
implementation also takes all the argument types that dicts take.

STeVe



More information about the Python-list mailing list