同一Python类中的调用方法,类方法,静态方法 [英] Calling method, classmethod, staticmethod in the same Python class
问题描述
从一个著名的例子中,我了解了Python类中方法,类方法和静态方法之间的区别。
From a famous example, I learned the difference between method, classmethod and staticmethod in a Python class.
class A(object):
def foo(self,x):
print "executing foo(%s,%s)"%(self,x)
@classmethod
def class_foo(cls,x):
print "executing class_foo(%s,%s)"%(cls,x)
@staticmethod
def static_foo(x):
print "executing static_foo(%s)"%x
# My Guesses
def My_Question(self,x):
self.foo(x)
A.class_foo(x)
A.static_foo(x)
a=A()
现在我是想知道如何调用方法 @classmethod
和 @
Now I am wondering, how to call a method, @classmethod
, and @staticmethod
inside the class.
我在上面的 My_Question
函数中输入了我的猜测,如果我对以上任何一个都不对,请纠正我。
I put my guesses in the My_Question
function above, please correct me if I am wrong with any of these.
推荐答案
是的,您的猜测会起作用。请注意,也有可能/通常在类的外部 调用静态方法和类方法:
Yes, your guesses will work. Note that it is also possible/normal to call staticmethods and classmethods outside the class:
class A():
...
A.class_foo()
A.static_foo()
还要注意, inside 常规实例方法通常是直接在实例上调用staticmethod和class方法( self
)而不是类( A
):
Also note that inside regular instance methods, it's customary to call the staticmethods and class methods directly on the instance (self
) rather than the class (A
):
class A():
def instance_method(self):
self.class_foo()
self.static_foo()
这允许继承按您期望的那样工作-如果我从创建
,如果我调用 B
子类A B.instance_method()
,我的 class_foo
函数将得到 B
代替 A
作为 cls
参数-并且可能,如果我在 B
上覆盖 static_foo
来做与<$ c $略有不同的事情c> A.static_foo ,这也将允许调用覆盖的版本。
This allow for inheritance to work as you might expect -- If I create a B
subclass from A
, if I call B.instance_method()
, my class_foo
function will get B
instead of A
as the cls
argument -- And possibly, if I override static_foo
on B
to do something slightly different than A.static_foo
, this will allow the overridden version to be called as well.
一些示例可能会更清楚:
Some examples might make this more clear:
class A(object):
@staticmethod
def static():
print("Static, in A")
@staticmethod
def staticoverride():
print("Static, in A, overrideable")
@classmethod
def clsmethod(cls):
print("class, in A", cls)
@classmethod
def clsmethodoverrideable(cls):
print("class, in A, overridable", cls)
def instance_method(self):
self.static()
self.staticoverride()
self.clsmethod()
self.clsmethodoverride()
class B(A):
@classmethod
def clsmethodoverrideable(cls):
print("class, in B, overridable", cls)
@staticmethod
def staticoverride():
print("Static, in B, overrideable")
a = A()
b = B()
a.instance_method()
b.instance_method()
...
运行完之后,尝试通过将所有自身更改。
更改为 A。
内 instance_method
。重新运行并进行比较。您会看到对 B
的所有引用都消失了(即使您正在调用 b.instance_method()
)。这就是为什么要使用 self
而不是类的原因。
After you've run that, try it by changing all of the self.
to A.
inside instance_method
. Rerun and compare. You'll see that all of the references to B
have gone (even when you're calling b.instance_method()
). This is why you want to use self
rather than the class.
这篇关于同一Python类中的调用方法,类方法,静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!