NameError:名称'self'未定义,即使它是?

时间:2014-01-07 21:56:31

标签: python class oop

任何人都可以帮助我理解为什么这会给我一个错误?错误是“NameError:name'self'未定义”。我的代码中有一个类似的类,并且工作正常吗?

我正在使用'xlrd',team是对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()]

4 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:0)

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

#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

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

答案 2 :(得分:0)

*

  

另一方面

*,如果您在构造函数( init )参数中忽略“self”关键字,那么您将获得:

    NameError: name 'self' is not defined

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

答案 3 :(得分:-2)

一个类的例子

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