Why this doesn't work?
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Fri Feb 19 10:17:30 EST 2010
mk a écrit :
> John Posner wrote:
>>> a
>>> False
>>>
>>> I expected to see 'nostatget' output: nostat.__get__ = nostatget
>>> obviously failed to replace this function's __get__ method.
>>
>> I don't quite understand the above sentence, so I'm assuming that you
>> wanted the final "is" test to be "True" instead of "False".
>
> No. I should have described my goal in plain English in first place.
>
> I wanted to replace nostat function's __get__ descriptor
A "descriptor" (shortcut for "object implementing the descriptor
protocol) is an object that have a __get__ method - not the __get__
method itself. A function is a descriptor. The __get__ method of the
function type is a method, and methods are not descriptors.
> with a function
> that would allow me to peek inside it (i.e. into __get__ descriptor
> arguments)
=> __get__(self, instance, cls=None)
When called as a result of a look up on an instance f of class Foo,
nostat.__get__ will get (nostat, f, Foo) as args.
> So no, I expected it to be false, that is, I expected to replace
> original descriptor with a "custom" descriptor.
s/descriptor/__get__/
Anyway: these types (function, method etc) are implemented in (higly
optimized) C, and with a lot of restriction for both performance and
sanity reasons. It's a case of practicality beats purity.
Another - perhaps more rewarding - exercise might be to implement a
custom callable type that implements the descriptor protocol the same
way the function do.
More information about the Python-list
mailing list