[Python-ideas] Shorthand syntax for get/set/delattr (was Re: Dict-like object with property access)
yoav glazner
yoavglazner at gmail.com
Sun Feb 5 13:58:08 CET 2012
On Sun, Feb 5, 2012 at 12:46 PM, Serhiy Storchaka <storchaka at gmail.com>wrote:
> 05.02.12 02:20, Nick Coghlan написав(ла):
>
>> It would be a *lot* cleaner if we could just use a normal assignment
>> statement instead of builtin functions to perform the name binding. As
>> it turns out, for ordinary instances, we can already do exactly that:
>>
>> for attr in "attr1 attr2 attr3".split():
>> vars(x)[attr] = vars(y)[attr]
>>
>> In short, I think proposals for dedicated syntax for dynamic attribute
>> access are misguided - instead, such efforts should go into enhancing
>> vars() to return objects that support *full* dict-style access to the
>> underlying object's attribute namespace (with descriptor protocol
>> support and all).
>>
>
> One-liner "def vars(v): return v.__dict__"?
>
This does't work for properties:
>>> class p:
@property
def pop(self): return 'corn'
>>> def vars(x): return x.__dict__
>>> p().pop
'corn'
>>> vars(p())['pop']
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
vars(p())['pop']
KeyError: 'pop'
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120205/f8b9dc9b/attachment.html>
More information about the Python-ideas
mailing list