python:NameError:全局名称'...'未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17557190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python: NameError:global name '...‘ is not defined
提问by Robert
in my code, I have:
在我的代码中,我有:
class A:
def a():
......
def b():
a()
......
b()
Then the compiler will say "NameError: global name a() is not defined." If I pull all the stuffs out of the class A, it would be no problem, but how can I define the method in class A? Thank you very much.
然后编译器会说“NameError: global name a() is not defined”。如果我把所有的东西都从A类中拉出来就没有问题了,但是我如何在A类中定义方法呢?非常感谢。
采纳答案by Martijn Pieters
You need to call self.a()
to invoke a
from b
. a
is nota global function, it is a method on the class.
您需要self.a()
调用a
从b
. a
是不是一个全球性的功能,它是在类中的方法。
You may want to read through the Python tutorial on classessome more to get the finer details down.
您可能需要阅读有关类的Python 教程,以了解更详细的信息。