Detect target name in descriptor __set__ method

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jul 22 20:05:55 EDT 2009


En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James  
<rhodri at wildebst.demon.co.uk> escribió:
> On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina  
> <gagsl-py2 at yahoo.com.ar> wrote:
>
>
>> class X(object):
>>    foo = descriptor()
>>
>> x = X()
>> x.foo = "value"
>
> Isn't this going to create a brand new instance attribute x.foo that has  
> nothing to do with the descriptor anyway?

No, it's up to the descriptor __set__ method what happens in this case.  
Think of the standard 'property' descriptor, the fset function can do  
whatever it wants.
Also, a data descriptor takes precedence over any instance attribute of  
the same name that might exist.

-- 
Gabriel Genellina




More information about the Python-list mailing list