2017-09-13 12 views
-1

나는 그래서 게임이 켜집니다 보드를 짓고 있어요 박하 사탕 정면 게임을 만들려고 정의 경우에도 AttributeError를 제기하지만, 나는이 오류 받고 있어요 :인스턴스 메소드는 속성이

Traceback (most recent call last): 
    File "python", line 18, in <module> 
    File "python", line 10, in display 
AttributeError: 'Board' object has no attribute 'cells 

import os #try to import the clode to the operating system, use import 
os.system('clear') 

# first: Build the board 
class Board(): #use class as a templete to create the object, in this case the board 
    def _init_(self): 
     self.cells = [' ', ' ', ' ' , ' ', ' ', ' ' , ' ', ' ', ' '] #will use self to define the method, in this case the board cells 
    def display(self): 
     print ('%s | %s | %s' %(self.cells[1] , self.cells[2] , self.cells[3])) 
     print ('_________') 
     print ('%s | %s | %s' %(self.cells[4] , self.cells[5] , self.cells[6])) 
     print ('_________') 
     print ('%s | %s | %s' %(self.cells[7] , self.cells[8] , self.cells[9])) 
     print ('_________') 


board = Board() 
board.display() 

답변

4
def _init_(self): 

요구가 될 수있는 문제의 원인을 파악 할 수 없습니다

def __init__(self): 
,

두 번 __에주의하십시오. 그렇지 않으면 호출되지 않습니다.


예를 들어 _init_ 기능으로이 클래스를 선택하십시오.

In [41]: class Foo: 
    ...:  def _init_(self): 
    ...:   print('init!') 
    ...:   

In [42]: x = Foo() 

아무 것도 인쇄되지 않습니다. 지금 생각해

In [43]: class Foo: 
    ...:  def __init__(self): 
    ...:   print('init!') 
    ...:   

In [44]: x = Foo() 
init! 

뭔가 __init__가 호출 된 수단을 인쇄된다는 사실을. 클래스는 __init__ 방법이없는 경우, 슈퍼 클래스 '__init__ (이 경우 object)이 우연히 아무것도하지 않고 어떤 속성을 인스턴스화하지 않는, 호출되는

참고.

+0

덕분에 많은 도움이되었습니다. –