Comments on: Python if/else in lambda http://eikke.com/python-ifelse-in-lambda/ 'cause this is what I do Tue, 04 Dec 2012 00:03:23 +0000 hourly 1 http://wordpress.org/?v=3.4.1 By: kracekumar http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-50594 kracekumar Sun, 22 May 2011 07:58:13 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-50594 I liked this article especially Eduardo de Oliveira Padoan comment I liked this article especially Eduardo de Oliveira Padoan comment

]]>
By: James http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-37385 James Sat, 10 Jul 2010 09:11:32 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-37385 Such an important article. God, this was killing me. Such an important article. God, this was killing me.

]]>
By: KingRadical http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-28744 KingRadical Wed, 04 Nov 2009 23:45:49 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-28744 >>> print (lambda x: np.array([3, 4]) if x == 2 else 1)(2) [3 4] >>> print (lambda x: np.array([3, 4]) if x == 2 else 1)(2)
[3 4]

]]>
By: KingRadical http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-28743 KingRadical Wed, 04 Nov 2009 23:44:42 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-28743 >>> print (lambda x: 0 if x == 2 else 1)(2) 0 >>> print (lambda x: 0 if x == 2 else 1)(3) 1 >>> print (lambda x: 0 if x == 2 else 1)(2)
0
>>> print (lambda x: 0 if x == 2 else 1)(3)
1

]]>
By: Guest http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-23934 Guest Wed, 10 Jun 2009 08:24:29 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-23934 (lambda x: x!=2 and 1 or 0) (lambda x: x!=2 and 1 or 0)

]]>
By: MA Nussli http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-7849 MA Nussli Mon, 11 Aug 2008 12:21:41 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-7849 The "And"/"Or" solution doesn't work in any case. At least, I didn't manage to make it work. For example, if your "True statement" is actually a value which is evaluated to a "False" expression(like 0, None, '' or []) then "False statement" will always be returned. example: print (lambda x:x==2 and 0 or 1)(2) ---> 1 print (lambda x:x==2 and 0 or 1)(3) ---> 1 Even worst, if one of the "True/False statements" cannot be evaluated to a boolean expression(like a numpy.array) then it won't work at all... example: print (lambda x:x==2 and numpy.array([3,4]) or 1)(2) Any idea to circumvent such problems?? The “And”/”Or” solution doesn’t work in any case. At least, I didn’t manage to make it work.

For example, if your “True statement” is actually a value which is evaluated to a “False” expression(like 0, None, ” or []) then “False statement” will always be returned.
example:
print (lambda x:x==2 and 0 or 1)(2) —> 1
print (lambda x:x==2 and 0 or 1)(3) —> 1

Even worst, if one of the “True/False statements” cannot be evaluated to a boolean expression(like a numpy.array) then it won’t work at all…
example:
print (lambda x:x==2 and numpy.array([3,4]) or 1)(2)

Any idea to circumvent such problems??

]]>
By: Adway http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-3315 Adway Mon, 12 May 2008 08:55:39 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-3315 I personally liked the 'and' 'or' idea. What's wrong about it? I personally liked the ‘and’ ‘or’ idea. What’s wrong about it?

]]>
By: Jo http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-897 Jo Sat, 23 Feb 2008 20:24:28 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-897 so after reading this it also means u could just do condition and "True statement" or "Falsestatement" which seams the easyest solution so after reading this it also means u could just do

condition and “True statement” or “Falsestatement”
which seams the easyest solution

]]>
By: Adriaan http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-855 Adriaan Thu, 21 Feb 2008 23:10:35 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-855 Looks like there's no real solid solution short of a helper function - like Marius said, ( and or ) fails if evaluates to False (the inverse, or and , is even worse), and Thomas' [, ][not ]) will always evaluate both and , which can be problematic... I'm starting to long for the ?: of c :-P Looks like there’s no real solid solution short of a helper function – like Marius said, ( and or ) fails if evaluates to False (the inverse, or and , is even worse), and Thomas’ [, ][not ]) will always evaluate both and , which can be problematic…

I’m starting to long for the ?: of c :-P

]]>
By: Marius Gedminas http://eikke.com/python-ifelse-in-lambda/comment-page-1/#comment-758 Marius Gedminas Sun, 17 Feb 2008 17:46:35 +0000 http://eikke.com/python-ifelse-in-lambda/#comment-758 Be very very careful about this X and Y or Z trick. It fails miserably when bool(Y) is False. Be very very careful about this X and Y or Z trick. It fails miserably when bool(Y) is False.

]]>