-1

I have a class called cell:

class cell:
def __init__(self, row, column, state):
    self.x = column * 20
    self.y = row * 20
    self.state = state

I have a list of cells which I create using nested for loops:

grid = []
for x in range(20):
    for y in range(20):
        grid.append(cell(x, y, bool(random.getrandbits(1))))

I can append cells to this grid by using cell() with no issues.

Later on, I have a subroutine called CalculateNextGrid() which creates a list of cells and returns it as an output.

def CalculateNextGrid(grid):
    output = []
    index = 0
    row = 0
    column = 0

    for cell in grid:
        print(grid)
        neighbors = CountCells(index, grid, 20)
        if cell.state == True:
            if neighbors < 2 or neighbors > 3:
                output.append(cell(row, column, False))
                grid.append(cell(x, y, bool(random.getrandbits(1))))
            else:
            output.append(cell(row, column, True))
        else:
            if neighbors == 3:
                output.append(cell(row, column, True))
            else:
                output.append(cell(row, column, False))
        row += 1
        if row == 20:
            row = 0
            column += 1
    return output

When I use output.append() to add cells to the list, python throws a "cell object not callable" error, despite it working in the code earlier on. Why is this?

cell object not callable

2
  • 3
    for cell in grid -- you are using the name cell for something else. (This is partly the reason you should capitalise class names, e.g. Cell) Commented Feb 1, 2018 at 14:45
  • Or rename grid to cells. for c in cells Commented Feb 1, 2018 at 14:48

1 Answer 1

3

Here

for cell in grid

you are using the name cell for something else. After that point, it no longer applies to the cell class.

You should capitalise your class name, Cell, so that it won't conflict with a separate cell variable.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.