control structures (was "Re: Sins")

skaller skaller at maxtal.com.au
Sun Jan 9 08:45:26 EST 2000


Darrell wrote:
> 
> 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

Yes, that provides the required functionality, but has a serious
problem: cases have to be defined 'out of line', and therefore
do not have access to the current scope. 

So I'm looking for a syntax for providing the above
functionality _inline_.

-- 
John (Max) Skaller, mailto:skaller at maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
voice: 61-2-9660-0850




More information about the Python-list mailing list