staticmethod problems

Neil Zanella nzanella at cs.mun.ca
Thu Aug 19 19:22:59 CEST 2004


Hello,

Coming from C++ and Java, one of the surprising things about Python is that
not only class instances (AKA instance objects) but also classes themselves
are objects in Python.

This means that variabls such as x and y appearing inside class statements
in Python essentially behave like static class variables in other languages.
On the other hand a variable is specified as an instance variable as self.x
and self.y inside a class in Python. Unqualified variables appearing inside
the class are class variables and not instance variables.

So now what I would like to do, is access the static variable of a superclass
object from a subclass object from a static method in the subclass object.

Here is what is not clear to me:
Why don't the commentd out lines below work? I was
expecting that a polymorphic search taking place when I
call Y.foo() would find both x and y, but this did not happen.
In particular, why does python force me to write something
like I write in foo3() to enforce the behavior I want in foo()?


Thanks,

Neil

#!/usr/bin/python

class X:
  x = 10

class Y(X):
  y = 5
  def foo():
    print x + y
  foo = staticmethod(foo)
  def foo2():
    print X.x + y
  foo2 = staticmethod(foo2)
  def foo3():
    print X.x + Y.y
  foo3= staticmethod(foo3)

#Y.foo() # I was expecting the same behavior as below,
         # with the value 15 printed. Why is this not
         # working (and where can I find more about
         # staticmethod)?
#Y.foo2() # doesn't work either. Why?

Y.foo3() # works

print Y.x # Since the object.attribute notation is used
          # polymorphism is used to locate attribute x.
          # Since class object Y does not contain x the
          # immediate parent X is searched and x found
          # therein. Finally the value of x is printed.

class XA:
  x = 10

class YA(XA):
  def foo(self):
    print self.x

YA().foo() # Since the object.attribute notation is used
           # polymorphism is used to locate attribute foo()
           # which is not found in instance object YA() so
           # class object YA is searched next and foo() is
           # found therein and run.
           #
           # Once run the object.attribute notation is used
           # to locate x which is not found in instance object
           # self (AKA YA()) so class object YA is searched and
           # since x is not found there either the immediate parent
           # class object XA in the inheritance hierarchy is searched
           # and attribute x is found therein.
           #
           # Once the x is found the object it references is printed.



More information about the Python-list mailing list