Issue 6740: Compounded expressions with lambda functions are evaluated incorrectly (original) (raw)
The compounded expressions with lambda functions are evaluated incorrectly. The simple expressions, or a named functions are evaluated good. The problem is only in the evaluation of compounded expressions. It seems that after evaluate of the first lambda function the evaluation of whole expression is stopped and not continue (see cond_error, which may raises the exception during evaluation).
Python 3.1 (r31:73572, Aug 15 2009, 22:04:19) [GCC 4.4.1 [gcc-4_4-branch revision 149935]] on linux2 Type "help", "copyright", "credits" or "license" for more information.
cond = (lambda x : x == 'foo') or (lambda x : x == 'bar') cond('foo') True cond('bar') False c1 = lambda x : x == 'foo' c1('foo') True c2 = lambda x : x == 'bar' c2('bar') True def ham(x): return x == 'foo' ... def spam(x): return x == 'bar' ... cond2 = lambda x : ham(x) or spam(x) cond2('foo') True cond2('bar') True cond2('ham') False cond_error = (lambda x : x == 'foo') or (lambda x : y == 'bar') cond_error('d') False
BTW: the same problem exists in Python 2.6.2 Python 2.6.2 (r262:71600, Aug 15 2009, 18:37:04) [GCC 4.4.1 [gcc-4_4-branch revision 149935]] on linux2 Type "help", "copyright", "credits" or "license" for more information.
This isn't the right forum to ask for help. You should try comp.lang.python, where someone would be happy to explain this to you.
Having said that, here's the explanation:
This is not a bug.
Disregarding side effects, the expression: a = b or c
is essentially the same as: if b: a = b else: a = c
So your code is: if (lambda x : x == 'foo'): cond = (lambda x : x == 'foo') else: cond = (lambda x : x == 'bar')
And since "(lambda x : x == 'foo')" evaluates to True (it's a lambda, so it's not None), you're really saying: cond = (lambda x : x == 'foo')
And your examples follow from that.
You can further verify this with your c1 and c2:
cond = c1 or c2 cond <function at 0x00B3EA30> cond is c1 True
So, you're just setting cond to the first of the 2 lambdas.