Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Instances in classes
#1
When I create 2 instances of a class and modify one, the other also gets modified. What am I doing wrong?

class superGrid:
    
    def __init__(self, square, size = 10):
        self.square =  square
        self.size = size
        
    def shuffle(self):
        self.square[3][7] = '#'
        
    def printGridSquare(self):
        print self.square[3][7]
    
class gridSquare(superGrid):
    new = 10*[10*['0']]
    
    size1 = len(new)
    def __init__(self, size = size1, square = new):
        self.square =  square
        self.size = size
        
# Create an instance of the grid square:
g = gridSquare()
s = gridSquare()
g.printGridSquare()
s.printGridSquare()
g.shuffle()
g.printGridSquare()
s.printGridSquare()
Reply
#2
Beginners mistake - both classes get copy of the same class variable SuperGrid.new, instead of new lists created per class.
Even worth - 10 * [10 * [0]]  - in Python, you usually put list on the left! - creates list of 10 references to innermost list

def __init__(self, square_size):
    self.square = [[0] * square_size for _ in range (square_size)]
will produce 10 individual lists of 10 individual sub-lists for each class - given that square_size is 10
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Many thanks for that - I don't think I would ever have found it without help.

It doesn't immediately solve my problem, as the new square is actually filled with 100 different initial values - the example I gave was the shortest code that exhibited the problem. At least I know where to look now.
Reply
#4
(Apr-20-2017, 03:01 PM)dannyH Wrote: Many thanks for that - I don't think I would ever have found it without help.

It doesn't immediately solve my problem, as the new square is actually filled with 100 different initial values - the example I gave was the shortest code that exhibited the problem.  At least I know where to look now.

Your are welcome. 
And you may use copy.deepcopy - if you want to use list of some fixed initial values for you square
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
Many thanks for that again. My actual code (not working yet) is:
class gridSquare(superGrid):

    new =    [['0',' ','F','O','W','E','K','T','A','H'],\
              ['1','A','G','O','X','E','L','U','B','I'],\
              ['2','A','H','P','Y','E','M','U','C','I'],\
              ['3','B','I','Q','Z','E','N','V','D','J'],\
              ['4','C','I','R',' ','F','O','W','E','K'],\
              ['5','D','J','S','A','G','O','X','E','L'],\
              ['6','E','K','T','A','H','P','Y','E','M'],\
              ['7','E','L','U','B','I','Q','Z','E','N'],\
              ['8','E','M','U','C','I','R',' ','F','O'],\
              ['9','E','N','V','D','J','S','A','G','O']]
    
    size1 = len(new)

    def __init__(self, size = size1, square = new):
        self.square =  square
        self.size = size
Reply
#6
1) You don't need backslashes between lines.
2) Instead of class-level init-variables, why not have all that in __init__, and have the default value be None and use the default values if nothing is passed?  That way you never have to deal with shared references, since the variables simply never exist outside of an instance.

Something like:
class gridSquare(object):
   def __init__(self, size = None, square = None):
       if square is None:
           square = [
                ['0',' ','F','O','W','E','K','T','A','H'],
                ['1','A','G','O','X','E','L','U','B','I'],
                ['2','A','H','P','Y','E','M','U','C','I'],
                ['3','B','I','Q','Z','E','N','V','D','J'],
                ['4','C','I','R',' ','F','O','W','E','K'],
                ['5','D','J','S','A','G','O','X','E','L'],
                ['6','E','K','T','A','H','P','Y','E','M'],
                ['7','E','L','U','B','I','Q','Z','E','N'],
                ['8','E','M','U','C','I','R',' ','F','O'],
                ['9','E','N','V','D','J','S','A','G','O']
           ]
       if size is None:
           size = len(square)
       self.square =  square
       self.size = size

grid = gridSquare()
print(grid.size) # 10
print(grid.square) # ...the grid
Reply
#7
Thanks. That looks like a safer solution.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 930 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Using classes? Can I just use classes to structure code? muteboy 5 5,036 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020