python:NameError:全局名称"..."未定义 [英] python: NameError:global name '...‘ is not defined

查看:93
本文介绍了python:NameError:全局名称"..."未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有:

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

    def b():
        a()
        ......
    b()

然后,编译器将说"NameError:未定义全局名称a()".如果我将所有东西都从A类中拉出来,那没问题,但是如何在A类中定义方法呢?非常感谢你.

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.

推荐答案

您需要调用self.a()才能从b调用a. a不是 全局函数,它是类上的方法.

You need to call self.a() to invoke a from b. a is not a global function, it is a method on the class.

您可能想通读有关类的Python教程,以了解更多内容了解更详细的信息.

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

这篇关于python:NameError:全局名称"..."未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆