Static class methods

Joshua Marshall jmarshal at mathworks.com
Mon Mar 5 17:47:54 EST 2001


Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl> wrote:
> Mon, 5 Mar 2001 01:06:06 +0100, Sven Peters <svpeters at t-online.de> pisze:

>> You CAN have class methods:
>> def bert:
>>         print "Bert"
>> class.method = bert
>> class.method()
>> "Bert"

> You can't. The first line is a syntax error. And the following:

> class C: pass
> def bert(): print "Bert"
> C.method = bert
> C.method()

> produces an error (unbound method must be called with class instance
> 1st argument).

But you can be super-ugly about it:

  >>> class C: pass
  ... 
  >>> def bert(): print "bert"
  ... 
  >>> C.method = bert
  >>> C.__dict__['method']()
  bert

or

  >>> C.method.im_func()
  bert

Although I can't say I recommend either of these approaches.



More information about the Python-list mailing list