if does not evaluate

Jim Newton jimka at rdrop.com
Mon Jun 7 16:28:56 EDT 2004


for compressions are quite nice but you are still pretty
limited about what you can put inside the for.  you cannot
put any code otherwise acceptable in the language.  For example
what if i need two, three or four statements inside the for?

another question, is why are "if" compressions not supported?

x = [ if some_function() else some_other_function() ]

or better

x = [ if    cond1():
       elsif cond2():
       else: cond3() ]

whouldn't that be inkeeping with the compression syntax?


-jim


Dan Bishop wrote:
> Jim Newton <jimka at rdrop.com> wrote in message news:<2if8daFmdreiU1 at uni-berlin.de>...
> 
>>how do you put an if or a for inside a lambda?
> 
>  
> I'm not quite sure what you'd expect "for" to evaluate to, but you
> might want to take a look at list comprehensions:
> 
>    [expr(var) for var in seq if cond(var)]
> 
> For "if", you can write "(F, T)[bool(C)]" or "(C and [T] or [F])[0]". 
> The latter has the advantage of short-circuiting.




More information about the Python-list mailing list