I'm a python addict !

afriere at yahoo.co.uk afriere at yahoo.co.uk
Thu Jan 29 02:15:23 EST 2009


On Jan 27, 4:52 am, Paul McGuire <pt... at austin.rr.com> wrote:
[snip]
>
>     # how you have to do it in C++ and Java
>     # light = light.next_state()
>
>     # using Python
>     light.__class__ = light.next_state

I'm sure you can, but why poke yourself in the eye with a blunt
stick? ;)

IMO there are two obvious problems with the latter approach.  Firstly
the information about the next state is left lying about in some
namespace separate from the object, whereas the object itself ought to
know what it's 'next_state' is (or, more correctly, how to determine
it).

Secondly you miss the opportunity of sending a signal to the new
state.  Consider that in some jurisdictions the amber light shows
before the green as well as before the red (yeah, I know is that an
invitation for drag racing or what!?).  If you called next_state as a
verb you could pass the current state as an argument and the amber (or
yellow, if you prefer) light could work which of the two possible next
states to call in turn.  The fact that this behaviour is, in this
example, peculiar to the amber light, demonstrates the pertinence of
my first objection above.

Fortunately the "C++ and Java" approach, (though I would never want to
be seen advocating the C++ or Java approach to anything), is in this
case available in Python as well, at the cheaper price of fewer
characters and arguably greater readibility to boot.

I know, Iknow ... you wanted to show how that code could be used in a
non "sulphurous" way.  Which just goes to show that the devil makes
work for idle hands ... or foils the best laid plans ... or is in the
detail ... or something sulphurous. :)



More information about the Python-list mailing list