nice python solution

Magnus Lie Hetland mlh at idi.ntnu.no
Sun Nov 18 18:43:47 EST 2001


"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

:)

> Greetings, Uwe.
>
>
> --
> Dr. Uwe Schmitt                                 Uwe.Schmitt at num.uni-sb.de

--
Magnus Lie Hetland                           The Anygui Project
http://hetland.org                           http://anygui.sf.net






More information about the Python-list mailing list