【问题标题】:NameError: name 'self' is not defined, even though it is?NameError:名称'self'未定义,即使它是?
【发布时间】:2014-01-25 19:03:06
【问题描述】:

谁能帮助我理解为什么这是给我一个错误?错误是“NameError: name 'self' is not defined”。我的代码中有一个类似的更高级别的课程并且效果很好?

我正在使用“xlrd”,并且团队是对 workbook.sheet_by_name 的引用。

class Rollout:                                  
    def __init__(self, team, name):
        self.team = team
        self.name = name
        self.jobs = {}
        self.start_row = 1
        self.last_row = self.team.nrows

    for i in range(self.start_row,self.last_row):
            try:
                self.jobs[i-1] =   [str(self.team.cell_value(i,0)).upper(), \
                                    str(self.team.cell_value(i,1)).upper(), \
                                    str(self.team.cell_value(i,2)).upper(), \
                                    str(self.team.cell_value(i,3)).upper(), \
                                    str(xlrd.xldate_as_tuple(self.team.cell_value(i,4),0)[3]), \
                                    str(self.team.cell_value(i,5)).upper(), \
                                    str(self.team.cell_value(i,6)).upper()]
            except ValueError:
                print "It look's like one of your 'time' cells is incorrect!"
                self.jobs[i-1] =   [str(self.team.cell_value(i,0)).upper(), \
                                    str(self.team.cell_value(i,1)).upper(), \
                                    str(self.team.cell_value(i,2)).upper(), \
                                    str(self.team.cell_value(i,3)).upper(), \
                                    "missing", \
                                    str(self.team.cell_value(i,5)).upper(), \
                                    str(self.team.cell_value(i,6)).upper()]

【问题讨论】:

  • 缩进正确吗?
  • for循环没有正确缩进
  • 如果 self 没有定义,那么你不在方法内..
  • 缩进是否正确?你也可以粘贴回溯吗?
  • 旁白:不需要在括号() 或括号[] 内使用\ 作为行继续。

标签: python class oop


【解决方案1】:

for 循环缩进不正确,导致它在该方法的范围之外但在类的范围内。这反过来意味着self 没有定义。

Python 确实在类的范围内解释该循环代码,但没有对象的实例。格式错误的示例代码:

class Simple(object):
    def __init__(self, a):
        self.a = a

    print("Here we go!")
    for i in xrange(self.a):
        print(i)

追溯

$ python simple.py
Here we go!
Traceback (most recent call last):
  File "simple.py", line 4, in <module>
    class Simple(object):
  File "simple.py", line 9, in Simple
    for i in xrange(self.a):
NameError: name 'self' is not defined

【讨论】:

  • Python 在 class 主体的执行范围内解释循环代码。在类范围内执行代码后,命名空间中的任何内容都将成为类对象的属性字典。如果因为self 而没有NameErrorSimple.i 最终将成为循环完成后i 最终设置的任何内容。
【解决方案2】:

如果您省略构造函数 (init) 参数中的“self”关键字,那么您将得到:

NameError: name 'self' is not defined

每当你在构造方法体中使用“self”时。

【讨论】:

    【解决方案3】:

    我在执行代码时遇到了同样的错误,

    #An example of a class
    class Shape:
        def __init__(self,x,y):
            self.x = x
            self.y = y
        description = "This shape has not been described yet"
        author = "Nobody has claimed to make this shape yet"
        def area(self):
            return self.x * self.y
        def perimeter(self):
            return 2 * self.x + 2 * self.y
        def describe(self,text):
            self.description = text
        def authorName(self,text):
            self.author = text
        def scaleSize(self,scale):
            self.x = self.x * scale
        self.y = self.y * scale
    

    然后我在代码的最后一行之前保留了空格

    #An example of a class
    class Shape:
        def __init__(self,x,y):
            self.x = x
            self.y = y
        description = "This shape has not been described yet"
        author = "Nobody has claimed to make this shape yet"
        def area(self):
            return self.x * self.y
        def perimeter(self):
            return 2 * self.x + 2 * self.y
        def describe(self,text):
            self.description = text
        def authorName(self,text):
            self.author = text
        def scaleSize(self,scale):
            self.x = self.x * scale
            self.y = self.y * scale
    

    然后没有错误,所以空间是问题..

    【讨论】:

    • 哥们,又是一个问题,你的难度和赠送的难度就像天上地下一样
    【解决方案4】:

    一个类的例子

    class Shape:
        def __init__(self,x,y):
            self.x = x
            self.y = y
        description = "This shape has not been described yet"
        author = "Nobody has claimed to make this shape yet"
        def area(self):
            return self.x * self.y
        def perimeter(self):
            return 2 * self.x + 2 * self.y
        def describe(self,text):
            self.description = text
        def authorName(self,text):
            self.author = text
        def scaleSize(self,scale):
            self.x = self.x * scale
            self.y = self.y * scale
    

    【讨论】:

    • 这不能回答 OP 的问题。您正在向他解释什么是班级,而这不是所要求的。请相应地删除/编辑您的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    相关资源
    最近更新 更多