[Tutor] Strange list behaviour in classes

C M Caine cmcaine at googlemail.com
Mon Feb 22 23:10:15 CET 2010


Or possibly strange list of object behaviour

IDLE 2.6.2
>>> class Player():
	hand = []

	
>>> Colin = Player()
>>> Alex = Player()
>>>
>>> Players = [Colin, Alex]
>>>
>>> def hands():
	for player in Players:
		player.hand.append("A")

>>> hands()
>>>
>>> Colin.hand
['A', 'A']
>>> Alex.hand
['A', 'A']

I would have expected hand for each object to be simply ['A']. Why
does this not occur and how would I implement the behaviour I
expected/want?

Thanks in advance for your help,
Colin Caine


More information about the Tutor mailing list