if the else short form

BartC bc at freeuk.com
Fri Oct 8 23:12:51 CEST 2010

"NevilleDNZ" <nevillednz at gmail.com> wrote in message
news:ad9841df-49a1-4c1b-95d0-e76b72df64eb at w9g2000prc.googlegroups.com...
> On Oct 7, 9:23 am, Lawrence D'Oliveiro <l... at geek-
> central.gen.new_zealand> wrote:
>> x = {1 : "One", 2 : "Two", 3 : "Three"}.get(i, "None Of The Above")
> More like:
> x = {1:lambda:"One", 2:lambda:"Two", 3:lambda:"Three"}.get(i,
> lambda:"None Of The Above")()
> i.e. deferred evaluation of selected case.
> In Algol68 this would be:
> x:=(i|"One","Two","Three"|"None Of The Above")

The point is, the construction works well when the syntax fully supports it.

When it needs the extra clutter that Python demands, it might not be so
worthwhile. Especially as it's not clear whether, even with lambdas, an
entire dictionary needs to be constructed before a selection can be made. In
fact a brief test showed an list+if (or is it tuple? I can never remember)
construction was much faster and much cleaner:

 x = ("One","Two","Three")[i-1] if 1<=i<=3 else "Other"

"NevilleDNZ" <nevillednz at gmail.com> wrote:
>"BartC" <b... at freeuk.com> wrote:

>> Probably there are workarounds here too, but I suspect the syntax won't
>> be
>> quite as pert as the Algol68-style example:
>> x = (i | "Zero", "One", "Two" | "None of the above")      # 0-based
> /* ¢ Algol68 case clause is 1-based. ¢ */

Yes, but my example was in Algol68-*style*: 0-based pseudo-syntax just in
case the Pythonians here couldn't get their heads around 1-based indexing..

(I've borrowed a lot from Algol68 when I've needed to design language syntax 
(naturally, just the best bits), and I do normally use 1-based.)


More information about the Python-list mailing list