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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:31:08  来源:igfitidea点击:

python: NameError:global name '...‘ is not defined

pythonnameerrorglobal-namespace

提问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 afrom b. ais nota global function, it is a method on the class.

您需要self.a()调用ab. a不是一个全球性的功能,它是在类中的方法。

You may want to read through the Python tutorial on classessome more to get the finer details down.

您可能需要阅读有关类Python 教程,以了解更详细的信息。