empty classes as c structs?

Alex Martelli aleaxit at yahoo.com
Sat Feb 5 05:07:11 EST 2005


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


Alex



More information about the Python-list mailing list