2014-12-31 2 views
0

긴 코드를 무시하지 마십시오. 정말 간단합니다. Basicaly 파이썬에서 Game of Life를하려고합니다. 여기에 오류가 발생합니다.'numpy.ndarray'객체를 호출 할 수 없습니다.

내가 neighbour_count() 함수를 호출하면 각 요소에있는 이웃 수를 올바르게 얻을 수 있습니다.

def neighbours_count(self): 

    neighbours_count = convolve2d(self.board, np.ones((3, 3)), 
            mode='same', boundary='wrap') - self.board          
    self.neighbours_count = neighbours_count 

그럼 내가 다음 단계를 만들고 싶어하고 수행하는 4 규칙에 따라 행동하고 게임이 제대로 진행 :

def make_step(self): 
    # We want to check the actual board and not the board that exists after eg. step 2. 
    self.board_new = np.zeros(shape=(self.size, self.size)) 

    # 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 
    mask = (self.board == 1) & (self.neighbours_count < 2) 
    self.board_new[mask] = 0 

    # 2. Any live cell with two or three live neighbours lives on to the next generation. 
    mask1 = (self.board == 1) & (self.neighbours_count == 2) 
    self.board_new[mask1] = 1 
    mask2 = (self.board == 1) & (self.neighbours_count == 3) 
    self.board_new[mask2] = 1   

    # 3. Any live cell with more than three live neighbours dies, as if by overcrowding. 
    mask = (self.board == 1) & (self.neighbours_count > 3) 
    self.board_new[mask] = 0 

    # 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. 
    mask = (self.board == 0) & (self.neighbours_count == 3) 
    self.board_new[mask] = 1 

    self.board = self.board_new 

을하지만, 내가 다시 같은 일을 할 때 (즉,) 이웃을 계산 한 후 나는 neighbour_count 함수를 호출 두 번째는 내가 얻을 :

TypeError: 'numpy.ndarray' object is not callable

나는 누구나하시기 바랍니다 도움이 될 수 있습니다 이것에 시간의 부당한 금액을 보냈습니다?

감사합니다.

답변

2

원래 neighbours_count은 방법 :

def neighbours_count(self): 

    neighbours_count = convolve2d(self.board, np.ones((3, 3)), 
            mode='same', boundary='wrap') - self.board          
    self.neighbours_count = neighbours_count 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

하지만 그렇게 할 때, (당신이했습니다하는 neighbours_count라고 또한 혼동 )을 convolve2d 함수의 결과로 표시된 라인에이 방법을 대체 당신은 그것을 다시 호출하려고 시도합니다. 당신은 방법을 얻지 못합니다. 당신은 가치를 얻습니다. 이는 ndarray이며, 그렇게 호출하지, 그리고 :

TypeError: 'numpy.ndarray' object is not callable 

나는 당신이 뭘하려는 건지 잘 모르겠지만, 당신이 어딘가에 값을 숨기고 싶다면, 사람들이 종종 하나의 밑줄을 사용, 예 self._neighbours_count.

+0

감사합니다. 매우 유용합니다! 나는 그 해답을 그렇게 빨리 기대하지 않았다. 나는 실제로 그 방법을 대체하고 있다는 것을 깨닫지 못했습니다. 다시 한 번 감사드립니다! –

관련 문제