classification
Title: Descriptor __get__() invoke is bypassed in the class context
Type: behavior Stage:
Components: None Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Stuart, benjamin.peterson
Priority: normal Keywords:

Created on 2013-01-21 17:49 by Stuart, last changed 2013-01-21 19:06 by benjamin.peterson. This issue is now closed.

Messages (2)
msg180346 - (view) Author: stuart (Stuart) Date: 2013-01-21 17:49
I emulated a real classmethod using python:

class cm(object):
    def __init__(self, o):
        self.o = o
    def __get__(self, obj, type=None):
        return self.o.__get__(obj, type)

then I check whether it is workable in the interactive mode and it is working:

Python 2.7.3 (default, Sep 26 2012, 21:53:58) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(cls):
...     print cls
... 
>>> cm = cm(foo)
>>> cm.__get__(int, type)
<bound method type.foo of <type 'int'>>

then I tried it in the real class but it failed:

>>> class C(object):
...     @cm
...     def foo(cls):
...         print cls
... 
>>> C.foo
<unbound method C.foo>
msg180351 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-01-21 19:06
If you want to emulate classmethod, calling __get__() on the function is the wrong thing to do. f.__get__(None, X) -> f You need to create a bound yourself instead.
History
Date User Action Args
2013-01-21 19:06:18benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg180351

resolution: not a bug
2013-01-21 17:49:12Stuartcreate