Python:从子类调用静态方法

时间:2015-02-11 14:39:25

标签: python static-methods

是否可以从子类中的方法调用超类中定义的静态方法?类似的东西:

class A:
   @staticmethod
   def a():
      ...

class B(A):
   def b(self):
      A.a()

A.a()不起作用,B.a(),super.a()或self.a()也不起作用。有没有办法做到这一点?

编辑: 问题是陈旧的.pyc文件!!!!!!

2 个答案:

答案 0 :(得分:0)

适用于我 - 除了super().whatever()当然只适用于Python 3.x.请解释一下“不工作”的意思......

Python 2.7.3 (default, Dec 18 2014, 19:10:20) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
...   @staticmethod
...   def foo(what):
...       print "Foo.foo(%s)" % what
... 
>>> class Bar(Foo):
...   def bar(self):
...       self.foo("from Bar.bar")
... 
>>> b = Bar()
>>> b.bar()
Foo.foo(from Bar.bar)
>>> Foo().foo("aaa")
Foo.foo(aaa)
>>> Foo.foo("aaa")
Foo.foo(aaa)
>>> b.foo("uuu")
Foo.foo(uuu)

答案 1 :(得分:-2)

>>> class ABC(object):
...     @staticmethod
...     def staticMethod(x):
...         print "in staticMethod:-", x
... 
>>> class B(ABC):
...     def b(self):
...         ABC.staticMethod("From B")
...         self.staticMethod("From B self") 
... 
>>> a = B()
>>> a.b()
in staticMethod:- From B
in staticMethod:- From B self