control structures (was "Re: Sins")
Darrell
darrell at dorb.com
Sat Jan 8 13:42:08 EST 2000
From: "skaller" <skaller at maxtal.com.au>
> What would be 'generally' useful is a genuine switch
> statement -- a sort of unrolled dictionary whose values
> are pieces of code. Syntax?
>
> switch x:
> case 1: ..
> case 2: ...
> default: ...
>
> like C, except that each handler has an implicit
> break at the end of the handler suite (no fall through,
> like in C).
>
> Comments?
This is a common thing, does this express the idea ?
def one(arg):
print 'ARGV:', arg,
return 'RetVal'
def two(arg):
print 'ARGV:', arg,
return 'RetVal'
states={1:one, 2:two, 3:3}
def switch(state, args, stateTable, default=None):
val=stateTable.get(state,default)
if callable(val):
return val(args)
return val
for x in range(5):
print 'State:', x, `switch(x, "argVal", states)`
####### Output
State: 0 None
State: 1 ARGV: argVal 'RetVal'
State: 2 ARGV: argVal 'RetVal'
State: 3 3
State: 4 None
--Darrell
More information about the Python-list
mailing list