Why this doesn't work?

John Posner jjposner at optimum.net
Fri Feb 19 00:57:10 CET 2010


On 2/18/2010 12:28 PM, mk wrote:
>
> Sorry to bother everyone again, but I have this problem bugging me:
>
> #!/usr/bin/python -i
>
> class Foo(object):
>
> def nostat(self,val):
> print val
>
> nostat.__orig_get__ = nostat.__get__
>
> @staticmethod
> def nostatget(*args, **kwargs):
> print 'args:', args, 'kwargs:', kwargs
> nostat.__orig_get__(*args, **kwargs)
>
> nostat.__get__ = nostatget
> setattr(nostat,'__get__',nostatget)
>
>
> f = Foo()
>
> f.nostat('a')
>
> print f.nostat.__get__ is f.nostat.__orig_get__
>
>
> This produces:
>
> 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".

It looks like you've replaced ...

  (A) the original __get__() method

... with ...

  (B) a new method that *calls* the original __get__() method

So why should you expect (A) and (B) to be the same object?

>
> The question is why? Isn't __get__ a normal attribute of a function nostat?
>
> This is made so much weirder that nostat.__get__ is no longer original
> __get__, so it looks like it should have been replaced, but if so, why
> nostatget doesn't get called?
>
> Regards,
> mk
>
>
>

-John



More information about the Python-list mailing list