[英]'numpy.ndarray' object is not callable
請不要忽略長代碼,這很簡單。 基本上,我正在嘗試用Python做一個人生游戲。 這是我得到錯誤的地方。
當我調用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”對象不可調用
我在此上花費了不合理的時間,有人可以幫忙嗎?
謝謝。
最初, 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
但是你用結果替換標記線這種方法convolve2d
函數(你容易混淆也稱為neighbours_count
),所以當你嘗試再次調用它,你沒有得到的方法,你得到的價值。 這是一個ndarray
,不可調用,因此:
TypeError: 'numpy.ndarray' object is not callable
我不確定您要做什么,但是如果您想在某個地方存儲一個值,人們通常會使用單個下划線,例如self._neighbours_count
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.