python: NameError:global name '…‘ is not defin

2019-01-17 06:03发布

This question already has an answer here:

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条回答
家丑人穷心不美
2楼-- · 2019-01-17 06:40

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.

查看更多
登录 后发表回答