0

I am trying to experiment with objects -- I want an object that can count the number of instances there are.

My code:

class Location:
    count = 0

    def __init__(self, column, row):
        self.column = column
        self.row = row
        self.num = Location.count
        Location.count += 1

    def position(self):
        print("Col: %i, Row: %i, Instance: %i" % (self.column, self.row, self.num))


# set up grid
grid = [[0]*5]*5

# Create objects in all positions
for i in range(0, len(grid)):
    for j in range(0, len(grid[i])):
        grid[i][j] = Location(i, j)
        grid[i][j].position()  #prints correctly



for i in range(0, len(grid)):
    for j in range(0, len(grid[i])):
        grid[i][j].position()  #prints incorrectly -- WHY?!?!

Why does it, when printing the contents of the grid the second time, show completely the wrong data?

I am essentially creating a grid of instances of an object class. I ant to be able to keep track of the number of instances in this object.

This code is not for anything in particular -- just for my own enjoyment and learning!

edit** OUTPUT OF CODE:

Col: 0, Row: 0, Instance: 0
Col: 0, Row: 1, Instance: 1
Col: 0, Row: 2, Instance: 2
Col: 0, Row: 3, Instance: 3
Col: 0, Row: 4, Instance: 4
Col: 1, Row: 0, Instance: 5
Col: 1, Row: 1, Instance: 6
Col: 1, Row: 2, Instance: 7
Col: 1, Row: 3, Instance: 8
Col: 1, Row: 4, Instance: 9
Col: 2, Row: 0, Instance: 10
Col: 2, Row: 1, Instance: 11
Col: 2, Row: 2, Instance: 12
Col: 2, Row: 3, Instance: 13
Col: 2, Row: 4, Instance: 14
Col: 3, Row: 0, Instance: 15
Col: 3, Row: 1, Instance: 16
Col: 3, Row: 2, Instance: 17
Col: 3, Row: 3, Instance: 18
Col: 3, Row: 4, Instance: 19
Col: 4, Row: 0, Instance: 20
Col: 4, Row: 1, Instance: 21
Col: 4, Row: 2, Instance: 22
Col: 4, Row: 3, Instance: 23
Col: 4, Row: 4, Instance: 24
Col: 4, Row: 0, Instance: 20
Col: 4, Row: 1, Instance: 21
Col: 4, Row: 2, Instance: 22
Col: 4, Row: 3, Instance: 23
Col: 4, Row: 4, Instance: 24
Col: 4, Row: 0, Instance: 20
Col: 4, Row: 1, Instance: 21
Col: 4, Row: 2, Instance: 22
Col: 4, Row: 3, Instance: 23
Col: 4, Row: 4, Instance: 24
Col: 4, Row: 0, Instance: 20
Col: 4, Row: 1, Instance: 21
Col: 4, Row: 2, Instance: 22
Col: 4, Row: 3, Instance: 23
Col: 4, Row: 4, Instance: 24
Col: 4, Row: 0, Instance: 20
Col: 4, Row: 1, Instance: 21
Col: 4, Row: 2, Instance: 22
Col: 4, Row: 3, Instance: 23
Col: 4, Row: 4, Instance: 24
Col: 4, Row: 0, Instance: 20
Col: 4, Row: 1, Instance: 21
Col: 4, Row: 2, Instance: 22
Col: 4, Row: 3, Instance: 23
Col: 4, Row: 4, Instance: 24

Thanks

Mr D
  • 25
  • 4

1 Answers1

1

The problem is not in your class, but in your grid; you are creating five copies of the same list of five instances.

Instead use a list comprehension:

grid = [[0]*5] for _ in range(5)]
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks...worked perfectly! I used a Python visualiser I could see it was always pointing to the same list and I couldn't work out why. Thanks again! – Mr D Feb 21 '17 at 12:09