[Python-Dev] Re: Static method and class method comments

Thomas Heller thomas.heller@ion-tof.com
Thu, 26 Jul 2001 11:22:59 +0200


From: "M.-A. Lemburg" <mal@lemburg.com>
> AFAIK, the only way to add classmethods to a class is by doing
> so after creation of the class object.
Wrong IMO:

C:\>c:\python22\python.exe
Python 2.2a1 (#21, Jul 18 2001, 04:25:46) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> class X:
...   def foo(*args): return args
...   goo = classmethod(foo)
...   global x
...   x = (foo, goo)
...
>>> print x
(<function foo at 007B786C>, <classmethod object at 007C7190>)
>>> print X.foo, X.goo
<unbound method X.foo> <bound method class.foo of <class __main__.X at 007B6664>>

The classmethod is created before the class is done,
it is converted into a method bound to the class
when you access it.

Thomas