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


Messages In This Thread
Instances in classes - by dannyH - Apr-20-2017, 01:02 PM
RE: Instances in classes - by volcano63 - Apr-20-2017, 02:28 PM
RE: Instances in classes - by dannyH - Apr-20-2017, 03:01 PM
RE: Instances in classes - by volcano63 - Apr-20-2017, 03:07 PM
RE: Instances in classes - by dannyH - Apr-20-2017, 03:24 PM
RE: Instances in classes - by nilamo - Apr-20-2017, 03:49 PM
RE: Instances in classes - by dannyH - Apr-20-2017, 05:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 974 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Using classes? Can I just use classes to structure code? muteboy 5 5,104 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