nice python solution

Steve Holden sholden at holdenweb.com
Mon Nov 19 15:29:09 CET 2001


"Magnus Lie Hetland" <mlh at idi.ntnu.no> wrote in message
news:9t9h3j$7r9$1 at tyfon.itea.ntnu.no...
> "Uwe Schmitt" <uwe at rocksport.de> wrote in message
> news:9t95gu$ab7l1$1 at hades.rz.uni-sb.de...
> > Hi,
> >
> > i there a nice python shortcut for:
> >
> >     if idx=0:
> >        a=val
> >     elif idx=1:
> >        b=val
>
> This isn't really an answer to your question, perhaps, but have
> you considered keeping the if-sentence? It's a very Pythonic and
> readable thing to do, IMO. Trying to find fancy shortcuts will
> often make your code less readable, although possibly more
> fancy ;)
>
> > ???
> > I tried
> >    (a,b)[idx] = val
> >
> > but this does not work, i get an error message.
>
> Well... If you want an evil solution:
>
>   locals()[['a','b'][idx]] = val
>
This is absolutely verboten. I quote from Section 2.3 of the 2.0 manual, but
I don't think anything has changed since: """locals ()
Return a dictionary representing the current local symbol table. Warning:
The contents of this dictionary should not be modified; changes may not
affect the values of local variables used by the interpreter.
"""

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list