2014-01-07 74 views
4

任何人都可以幫助我理解爲什麼這是給我一個錯誤?錯誤是「NameError:name'self'未定義」。我的代碼中有更高的類似類,並且工作正常?NameError:name'self'沒有被定義,即使它是?

我使用'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()] 
+4

縮進是否正確? –

+3

for循環沒有正確縮進 –

+2

如果'self'沒有定義,那麼你不在裏面的方法.. –

回答

10

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 
+0

Python在'class'主體的執行範圍內解釋循環代碼。無論在類範圍內執行代碼之後名稱空間中的任何內容成爲類對象的屬性字典。如果因爲self而沒有'NameError','Simple.i'將最終成爲循環完成後我設置的任何內容。 –

1

我在執行我的代碼得到了同樣的錯誤,

#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 

然後得到n O錯誤,所以空間是問題..

0

*

By other hand

*,如果你省略在構造函數的(初始化)的說法 「自我」 的關鍵字,那麼你將得到:

NameError: name 'self' is not defined 

每當在構造函數方法體中使用「self」時。

相關問題