python: NameError:global name '…‘ is not defined [duplicate]

三世轮回 提交于 2019-11-27 01:14:27

问题


This question already has an answer here:

  • Python: NameError: global name 'foobar' is not defined [duplicate] 1 answer

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.


回答1:


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

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



来源:https://stackoverflow.com/questions/17557190/python-nameerror-global-name-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!