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

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Dec 6 01:50:41 CET 2012


On 5 December 2012 18:11, C M Caine <cmcaine at googlemail.com> wrote:
> 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)

>From your description above I have very little idea what you're trying
to do. You have specified what you were expecting to happen why you're
not happy with what actually happened, which is good. I still don't
understand the problem, though. What is the *relevant* code that
didn't do what you expected?

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

I don't know what a Kalah board is.

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

You were right not to post this code directly in your email as it's
too big. For the same reason, though, I'm not prepared to read it
through and try to understand the problem.

It would be better if you could trim your problem down to a short
example so that you can then post the full example. An important side
effect of this process is that you will often discover the cause of
the problem yourself before completing your email to the list.

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

Probably at least one of the above is true, but I can't say much more
than that. Have a read of http://sscce.org/ for some advice about how
to post problems to a mailing list. If you follow the advice there you
will find that
1) You will often be able to solve your problem yourself.
2) If you do post your problem to a mailing list you will be more
likely to get a helpful response.


Oscar


More information about the Tutor mailing list