[英]Class method can not call other class methods within the same class
I have a class defined with two methods: 我有一个用两种方法定义的类:
class A:
def called():
print 'called'
def caller(self):
called()
But caller can not use called directly 但是来电者不能直接使用被叫
A().caller()
gives error 给出错误
NameError: global name 'called' is not defined
How can I call the other unbounded method within the same class ? 如何在同一类中调用其他无界方法?
Qualify the method with self
or the class name A
. 用
self
或类名A
限定方法。
class A:
@staticmethod
def called():
print 'called'
def caller(self):
self.called()
# Or
A.called()
NOTE I changed the method called
as a static method. 注意我更改了
called
静态方法的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.