【问题标题】:NameError: name is not defined in python init function [closed]NameError:名称未在python init函数中定义[关闭]
【发布时间】:2016-08-23 08:58:50
【问题描述】:
class HumidityServer(CoAP):
    def __init__(self, host, port, noOfSensors=10, multicast=False):
        CoAP.__init__(self, (host, port), multicast)

        for num in range(noOfSensors):
            self.add_resource('humidity'+num+'/', HumidityResource(num))

此摘录是生成以下程序的程序的一部分:

Traceback (most recent call last):
  File "humidityserver.py", line 10, in <module>
    class HumidityServer(CoAP):
  File "humidityserver.py", line 14, in HumidityServer
    for num in range(noOfSensors):
NameError: name 'noOfSensors' is not defined

即使我为变量定义了默认值,为什么会发生这种情况?

【问题讨论】:

    标签: python init nameerror


    【解决方案1】:

    您在代码中混合了制表符和空格;这是您粘贴到问题中的原始代码:

    灰色实线是制表符,点是空格。

    注意for 循环如何缩进为 8 个空格,但def __init__ 是缩进一个制表符? Python 将制表符扩展到 八个 空格,而不是四个,因此对于 Python,您的代码看起来像这样:

    现在您可以看到for 循环在__init__ 方法的外部,并且__init__ 函数签名中的noOfSensors 变量没有在那里定义。

    不要在缩进中混合制表符和空格,坚持仅制表符或仅个空格。 PEP 8 Python 风格指南strongly advises you to use only spaces for indentation。例如,您的编辑器可以轻松配置为在您使用 TAB 键时插入空格。

    【讨论】:

    • @Sandi:Sublime Text 3,使用 Monokay 配色方案。
    【解决方案2】:

    我复制并运行了代码,这不是因为@Martijn 回答的混合制表符和空格问题。我刚刚在创建基于类的小游戏时遇到了类似的问题。

    即使我已经为变量分配了默认值,但它卡住并给了我错误:

    NameError: name 'mental' is not defined #where mental is the variable
    

    我研究了一下,看到有人在谈论实例。然后我尝试创建一个实例并让实例执行该函数,同时我定义了一个我打算执行的函数。它成功了。以下是我的修复示例:

    class People(object):
        def __init__(self, vital, mental):
        self.vital = vital
        self.mental = mental
    
    class Andy(People):
            print "My name is Andy... I am not the killer! Trust me..."
            chat1 = raw_input(">")
            if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
                self.mental += -1
                print "(checking) option 1"
            elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don\'t believe' or 'i don\'t trust you'):
                self.mental += 1
                print "(checking) option 2"
            else:
                print "Pass to else"
                print self.mental
                print self.vital
    
    andy = Andy(1, 5)
    

    我找到的解决方案是:

    class People(object):
        def __init__(self, vital, mental):
        self.vital = vital
        self.mental = mental
    
    class Andy(People):
        def play(self):
            print "My name is Andy... I am not the killer! Trust me..."
            chat1 = raw_input(">")
            if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
                self.mental += -1
                print "(checking) option 1"
            elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don\'t believe' or 'i don\'t trust you'):
                self.mental += 1
                print "(checking) option 2"
            else:
                print "Pass to else"
                print self.mental
                print self.vital
    
    andy = Andy(1, 5)
    andy.play()
    

    也许您的问题还有其他解决方案,但我是编程新手,您的代码中有一些我不明白的东西。但是关于你得到的错误,我认为它是因为'self'必须是你设置的一个实例,它才能在类中运行。如果我的概念有误,请纠正我。

    【讨论】:

      【解决方案3】:

      它还没有被实例化,所以在创建类时执行的默认值 man

      【讨论】:

        猜你喜欢
        • 2023-03-05
        • 1970-01-01
        • 2021-08-23
        • 2016-03-11
        • 2016-12-20
        • 2020-12-23
        • 2014-03-18
        • 2021-07-27
        • 1970-01-01
        相关资源
        最近更新 更多