Create static method dynamically

Salvatore salvatore at nolog.org
Thu Mar 11 17:04:43 EST 2004


Hello,

Here is my problem :

class myClass:
     def addmethod(name,method):
         maClasse.__dict__[name] = method
     addmethod = staticmethod(addmethod)

def hello():
     print "hello"

myClasse.addmethod('hello',hello)


Traceback (most recent call last):
   File "<pyshell#0>", line 1, in -toplevel-
     myClass.hello()
TypeError: unbound method hello() must be called with myClass instance 
as first argument (got nothing instead)

I would like to make this call:
myClass.hello()

Is there a way of making 'hello' a
static method of 'myClass ?

Regards




More information about the Python-list mailing list