简体   繁体   English

'numpy.ndarray'对象不可调用

[英]'numpy.ndarray' object is not callable

Please don't disregard the long code, it is really simple. 请不要忽略长代码,这很简单。 Basicaly I am trying to do a Game of Life in Python. 基本上,我正在尝试用Python做一个人生游戏。 Here is where I get the error. 这是我得到错误的地方。

When I call the neighbour_count() function I can correctly get the number of neighbours that each element have. 当我调用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   

Then I want to make the next step and act on 4 rules, which it does and the game advances correctly: 然后,我要进行下一步,并按照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

However, when I want to DO THE SAME THING AGAIN (ie count the neighbours), then the second time I call the neighbour_count function I get: 但是,当我想再次做同样的事情(即计数邻居)时,那么我第二次调用neighbour_count函数,我得到:

TypeError: 'numpy.ndarray' object is not callable TypeError:“ numpy.ndarray”对象不可调用

I have spent unreasonable amount of time on this, can anyone help please? 我在此上花费了不合理的时间,有人可以帮忙吗?

Thanks. 谢谢。

Originally, neighbours_count is a method: 最初, 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   
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

But then you replace this method in the marked line by the result of the convolve2d function (which you've confusingly also called neighbours_count ), so when you try to call it again, you don't get the method, you get the value. 但是你用结果替换标记线这种方法convolve2d函数(你容易混淆称为neighbours_count ),所以当你尝试再次调用它,你没有得到的方法,你得到的价值。 This is an ndarray , and it's not callable, and so: 这是一个ndarray ,不可调用,因此:

TypeError: 'numpy.ndarray' object is not callable

I'm not sure what you're trying to do, but if you want to stash a value somewhere, people often use a single underscore, eg self._neighbours_count . 我不确定您要做什么,但是如果您想在某个地方存储一个值,人们通常会使用单个下划线,例如self._neighbours_count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM