未定义Python NameError(def) [英] Python NameError (def) is not defined

查看:102
本文介绍了未定义Python NameError(def)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用以下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()

运行此代码时,出现NameError:未定义名称"method1".如何解决此错误?

When I run this code, I get NameError: name 'method1' is not defined. How do I resolve this error?

推荐答案

只需添加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):

这篇关于未定义Python NameError(def)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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