[Python-ideas] Assignment decorators (Re: The Descriptor Protocol...)

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Mar 6 00:01:26 CET 2011


Ethan Furman wrote:

> class Person():
>     name = Str()
>     address = Str()
> 
> def Str():
>     whatever = __var_name__  # 'name' in first call, 'address' in second
>     ...

Hmmm, that seems rather convoluted, and it's not clear what should
happen in more complex cases. What if there is more than one function
call in the rhs expression? Which one gets the __var_name__? Do any
of them?

I suppose the answer would be that it only applies when the top level
of the rhs consists of a single function call.

It's also not clear how to implement this. Setting it as an attribute
of the function itself would be wrong -- it really needs to be injected
into the namespace of the frame somehow during the call.

-- 
Greg



More information about the Python-ideas mailing list