[Tutor] class methods as argument

Andrei project5 at redrival.net
Sat Feb 10 13:56:49 CET 2007


Hi Thomas,


thomas coopman wrote:
<snip>
> I want to execute the class method given as argument,
> but this obvious doesn't work, but I don't know how to
> get it work,

Pass the method reference of the class as argument, like this:

 >>> class A(object):
...     def __init__(self, x): self.x = x
...     def dostuff(self, arg):
...         print self.x
...         print arg
...
 >>> def dostuff(obj, method):
...     method(obj, 'blabla')
...
 >>> a = A(5)
 >>> dostuff(a, A.dostuff)
5 # this demonstates method has been called for instance a
blabla




Yours,

Andrei



More information about the Tutor mailing list