Python 名称错误:名称';自我';没有定义,即使它是?
有人能帮我理解为什么这会给我一个错误吗?错误为“NameError:未定义名称‘self’”。我有一个类似的类在我的代码中更高的位置,这可以正常工作吗 我使用的是'xlrd',team是对workbook.sheet\u by\u名称的引用Python 名称错误:名称';自我';没有定义,即使它是?,python,class,oop,Python,Class,Oop,有人能帮我理解为什么这会给我一个错误吗?错误为“NameError:未定义名称‘self’”。我有一个类似的类在我的代码中更高的位置,这可以正常工作吗 我使用的是'xlrd',team是对workbook.sheet\u by\u名称的引用 class Rollout: def __init__(self, team, name): self.team = team self.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()]
循环的
缩进不正确,导致它在该方法的作用域之外,但在类的作用域之内。这反过来意味着未定义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 simple.py
我们走!
回溯(最近一次呼叫最后一次):
文件“simple.py”,第4行,在
类简单(对象):
文件“simple.py”,第9行,简单格式
对于xrange中的i(self.a):
NameError:未定义名称“self”
我在执行代码时遇到了相同的错误
#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
然后没有错误,所以空间是问题。如果您在构造函数的(init)参数中省略了“self”关键字,那么您将得到:
NameError: name 'self' is not defined
每当您在构造函数方法体中使用“self”时。类的示例
类形状:
定义初始化(self,x,y):
self.x=x
self.y=y
description=“尚未描述此形状”
author=“还没有人声称制作过这种形状”
def区域(自身):
返回self.x*self.y
def周界(自身):
返回2*self.x+2*self.y
def描述(自我,文本):
self.description=文本
def authorName(自我,文本):
self.author=文本
def缩放大小(自身,缩放):
self.x=self.x*刻度
self.y=self.y*刻度
缩进是否正确?for循环未正确缩进如果未定义self
,则说明您不在方法内部。缩进是否正确?您也可以粘贴回溯吗?旁白:不需要在括号()
或括号[]
内使用\作为行延续。Python在类的执行范围内解释循环代码。在类作用域中执行代码后在命名空间中结束的内容将成为类对象的属性字典。如果没有因为self
而出现namererror
,Simple.i
将在循环完成后成为i
设置的对象。这并不能回答OP的问题。你在向他解释什么是课堂,而不是被问到的。请删除/编辑你的答案,这是另一个问题,你的答案和呈现的答案之间的区别就像天空和地球之间的区别