Static method object not callable

Edward Diener eldiener at earthlink.net
Thu Aug 12 04:07:31 CEST 2004


Bengt Richter wrote:
> On Wed, 11 Aug 2004 02:40:07 GMT, "Edward Diener"
> <eldiener at earthlink.net> wrote:
>
>> Paul Morrow wrote:
>>> 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.
> By 'reference' are you referring to the Y in ad?
>>>>
Yes. I understand now why it won't work but I think this is a limitation of
Python. Evidently, if I set up my class attribute 'ad' to be "ad = { 1 :
X.Y }", then calling static method X.Y through "self.ad[1](3)" would work.
But Python does not allow me to say 'X.Y' from within a class attribute of X
because X has not been fully defined as a class at that time. As one person
answered, I can say "X.ad = { 1 : X.Y }" after the definition of class X and
that works fine. But that is very kludgy to me, forcing me to wait until the
definition of X is finished in order to create my ad class attribute.

I thank you very much for the explanation of _get_ and how static methods
work in Python. It still feels unnatural not to be able to reference a class
attribute of a class in another class attribute of the same class from
within the class definition.





More information about the Python-list mailing list