On Sun, Feb 5, 2012 at 12:46 PM, Serhiy Storchaka
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 "
", line 1, in <module> vars(p())['pop'] KeyError: 'pop'