[Tutor] Unexpected results with obj.method().method()

C M Caine cmcaine at googlemail.com
Wed Dec 5 19:11:12 CET 2012


Dear all,

I've written a class State that subclasses tuple. The class has a method
move_state that takes a move and returns a new state object representing
the new state of the game.

I would expect S1 and S3 to be equal on the last line here, but they are
not.

>>> import game
>>> S = game.State()
>>> S1 = S.move_state(1).move_state("SWAP")
>>> S2 = S.move_state(1)
>>> S3 = S2.move_state("SWAP")
>>> S1 == S3
False

Printing the two states shows that they have very different internal states.

>>> print S1
 (8, 8, 8, 8, 8, 8, 0)
1                     0
 (7, 7, 7, 7, 7, 7, 7)
>>> print S3
 (7, 7, 7, 7, 7, 7, 7)
0                     1
 (0, 8, 8, 8, 8, 8, 8)

If anyone is interested, State represents the state of a 7 7 Kalah board.

The full code is on pastebin http://pastebin.com/tUh0W5Se

Are my expectations faulty? (I hope not)
Have I made some mistake in my code to get these results?

Thanks in advance,
Colin Caine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121205/1dd4bd19/attachment.html>


More information about the Tutor mailing list