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

问题描述

我在使用以下Python代码时遇到了麻烦:

I'm having trouble with the following Python code:

class Methods:

    def method1(n):
        #method1 code

    def method2(N):
        #some method2 code
            for number in method1(1):
                #more method2 code

def main():
    m = Methods
    for number in m.method2(4):
            #conditional code goes here

if __name__ == '__main__':
    main()

运行此代码时,我得到

如何解决此错误?

推荐答案

只需添加self.在它前面:

Just add self. in front of it:

self.method1(1)

还将您的方法签名更改为:

Also change your method signitures to:

def method1(self, n):

def method2(self, n):

这篇关于如何解决"NameError:名称方法名称未定义"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:19
查看更多