control structures (was "Re: Sins")
Vadim Chugunov
chega at my-deja.com
Sun Jan 9 17:21:24 EST 2000
skaller <skaller at maxtal.com.au> wrote in message
news:387890F6.2D1136F8 at maxtal.com.au...
> Darrell wrote:
> >
> > From: "skaller" <skaller at maxtal.com.au>
>> ...
> > 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_.
Well, you could do this:
states= {
1: compile("x=x+1","<string>","single"),
2: compile("x=x+2","<string>","single")
}
# and later...
x = 0
exec states[y] # executes in locals AND modifies the calling context
print x
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list