Anomaly in creating class methods

venk venkatasubramanian at gmail.com
Thu Nov 3 06:19:22 EST 2005


Hi,
 given below is my interaction with the interpreter.... In one case, i
have created the class method using the "famous idiom"... and in the
other, i have tried to create it outside the class definition... why
isn't the latter working ? (of course, the presence of decorators is a
different issue)....
>>> class D:
...     def f(cls):
...             print cls
...
>>> D.f=classmethod(D.f)
>>> D.f
<bound method classobj.f of <class __main__.D at 0xb7c99f5c>>
>>> D.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method f() must be called with D instance as first
argument (got classobj instance instead)
>>> class D:
...     def f(cls):
...             print cls
...     f=classmethod(f)
...
>>> D.f
<bound method classobj.f of <class __main__.D at 0xb7c9947c>>
>>> D.f()
__main__.D




More information about the Python-list mailing list