Python NameError: name 'self' 未定义,即使它是?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20982780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NameError: name 'self' is not defined, even though it is?
提问by Paul Bentham
Can anyone helps me understand why this is this giving me an error? The error being "NameError: name 'self' is not defined". I have a similar class higher up in my code and that works fine?
谁能帮助我理解为什么这会给我一个错误?错误是“NameError: name 'self' is not defined”。我的代码中有一个类似的类,并且工作正常吗?
I'm using 'xlrd' and team is a reference to a workbook.sheet_by_name.
我正在使用“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()]
采纳答案by Kyle Kelley
The forloop is indented incorrectly resulting in it being outside that method's scope but inside the class' scope. This in turn means that selfis not defined.
该for循环是缩进错误地导致它是该方法的范围外,但类范围内。这反过来意味着self未定义。
Python does interpret that loop code in the scope of the class, but without an instance of the object. Sample malformed code:
Python 确实在类的范围内解释该循环代码,但没有对象的实例。示例格式错误的代码:
class Simple(object):
def __init__(self, a):
self.a = a
print("Here we go!")
for i in xrange(self.a):
print(i)
Traceback
追溯
$ 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
回答by Arulraj
I got the same error while executing my code,
我在执行我的代码时遇到了同样的错误,
#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
Then i kept space before last line of the codes like
然后我在最后一行代码之前保留了空间,例如
#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
Then got no error, So space is the problem..
然后没有错误,所以空间是问题..
回答by Yekatandilburg
*
*
By other hand
另一方面
*, if you omitthe "self" keyword in your Constructor's ( init) argument, then you will get:
*,如果您在构造函数 ( init) 参数中省略“self”关键字,那么您将得到:
NameError: name 'self' is not defined
Whenever you use "self" in the constructor method body.
每当您在构造函数方法体中使用“self”时。

