Static method object not callable

Shalabh Chaturvedi shalabh at cafepy.com
Tue Aug 10 23:26:38 EDT 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.

A staticmethod has to be accessed from the class or an instance. But you can
put the 'bare' function in your dictionary before you make a staticmethod
out of it:

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

This prints:

3
Done.

HTH,
Shalabh

        




More information about the Python-list mailing list