Alex Martelli wrote:
Nick Coghlan <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list