Static method object not callable

Paul Morrow pm_mon at yahoo.com
Wed Aug 11 03:57:58 CEST 2004


Edward Diener wrote:
> This simple code example gives me the message, "TypeError: 'staticmethod'
> object is not callable".
> 
> class X(object):
>   def Y(x):
>     print x
>   Y = staticmethod(Y)
>   ad = { 1 : Y }
>   def Z(self):
>     self.ad[1](3)
> x = X()
> x.Z()
> print "Done."
> 
> I know there must be a way to have a class attribute reference a static
> method, and then call that static method through the reference, so if anyone
> can correct this it would be appreciated.
> 
> 
> 

Here are two alternatives.

class X(object):
   def Y(x):
     print x
   Y = staticmethod(Y)
   ad = { 1 : 'Y' }
   def Z(self):
     getattr(self, self.ad[1])(3)
x = X()
x.Z()
print "Done."


class X(object):
   def Y(x):
     print x
   Y = staticmethod(Y)
   def __init__(self):
     self.ad = { 1 : self.Y }
   def Z(self):
     self.ad[1](3)
x = X()
x.Z()
print "Done."




More information about the Python-list mailing list