[Python-Dev] Summary of "dynamic attribute access" discussion
Jean-Paul Calderone
exarkun at divmod.com
Tue Feb 13 18:03:13 CET 2007
On Tue, 13 Feb 2007 17:20:02 +0100, "\"Martin v. Löwis\"" <martin at v.loewis.de> wrote:
>Anthony Baxter schrieb:
>>> and the "wrapper class" idea of Nick Coghlan:
>>> attrview(obj)[foo]
>>
>> This also appeals - partly because it's not magic syntax <wink>
>
>I also like this. I would like to spell it attrs, and
>I think its specification is
>
>class attrs:
> def __init__(self, obj):
> self.obj = obj
> def __getitem__(self, name):
> return getattr(self.obj, name)
> def __setitem__(self, name, value):
> return setattr(self.obj, name, value)
> def __delitem__(self, name):
> return delattr(self, name)
> def __contains__(self, name):
> return hasattr(self, name)
>
>It's so easy people can include in their code for backwards
>compatibility; in Python 2.6, it could be a highly-efficient
>builtin (you still pay for the lookup of the name 'attrs',
>of course).
This looks nice. The simplicity of the implementation is
great too.
Jean-Paul
More information about the Python-Dev
mailing list