[Tutor] Methods defined in my class are affecting all the objects at runtime.
Brian Stovall
hoppenmaus at gmail.com
Sat Oct 29 16:43:04 CEST 2011
Hello world!
I obviously don't understand something important and basic, but I am
having trouble figuring it out myself... I am running python v3.2.2 on
a Win XP machine.
My code:
import card_model
import random
class Pile:
"""An Object reperesenting a list of 'Card' Objects: could be a
hand, discard pile, tableau
or anything really. Has a display_type that is used to clarify
what kind of display to use, and
.deal(cards[] , new_pile(Pile Object)) and .shuffle() methods."""
DISPLAY_TYPES = ["STACK", "FAN", "ACCORDION", "CASCADE"]
def __init__(self, cards = [], display_type = "STACK"):
self.cards = cards
self.display_type = display_type
def __str__(self):
return_string = ""
for i in self.cards:
return_string = return_string + str(i) + "\n"
return_string = return_string + str(self.display_type)
return return_string
def shuffle(self):
random.shuffle(self.cards)
def add(self, card_list):
for i in card_list:
self.cards.append(i)
def deal(self, number_of_cards, position = 0):
"""Deletes the number of cards out of the pile, starting from
position (default is the top) and returns that list of cards, for
communication with other piles' .add methods."""
dealt_list = []
try:
for i in range(number_of_cards):
dealt_list.append(self.cards[position])
del self.cards[position]
return(dealt_list)
except IndexError:
print("Error, out of cards!")
return(None)
I had been testing it with single objects favorably, but when I
instantiate two Pile objects, methods like .add or .shuffle affect all
of the Pile objects in memory. At first I thought the objects were all
initializing to the same space in memory, but it wasn't true. If you
need to see all my modules or my tester code, I will happily post.
Thanks for helping a rank beginner!
-Brian
More information about the Tutor
mailing list