[Tutor] constants, flags or whatever
Steve Willoughby
steve at alchemy.com
Thu Dec 20 05:44:47 CET 2007
Dave Kuhlman wrote:
> On Wed, Dec 19, 2007 at 09:41:13PM -0500, bob gailer wrote:
>
>> 1 - I see no value in introducing variables. I'd just use string constants:
>>
>> action = "moving"
>> .
>> .
>> if action == "jumping":
>>
>> etc.
>
> I agree. But, some people do prefer something that looks a bit
> like an enum. If so, here is a Python idiom for enums:
One big advantage to using variables over string constants is that
there is a big danger of misspelling one of the constants and not
noticing the error.
action = "moving"
...
if action == "moveing":
# oops, this never gets executed
if you had used variables, Python would catch the error as an
attempt to use an undefined variable:
action = MOVING
...
if action == MOVEING: <-- error caught by python
> Mode_none, Mode_moving, Mode_inserting, Mode_jumping = range(4)
>
> action = Mode_moving
>
> if action == Mode_jumping:
> o
> o
> o
>
> - Dave
>
>
More information about the Tutor
mailing list