classification
Title: Compounded expressions with lambda functions are evaluated incorrectly
Type: behavior Stage: resolved
Components: Versions: Python 3.1
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, mvyskocil
Priority: normal Keywords:

Created on 2009-08-20 10:27 by mvyskocil, last changed 2009-08-20 16:12 by eric.smith. This issue is now closed.

Messages (2)
msg91766 - (view) Author: Michal Vyskocil (mvyskocil) Date: 2009-08-20 10:27
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.
msg91779 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-08-20 16:12
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 <lambda> at 0x00B3EA30>
>>> cond is c1
True

So, you're just setting cond to the first of the 2 lambdas.
History
Date User Action Args
2009-08-20 16:12:25eric.smithsetstatus: open -> closed

type: behavior

nosy: + eric.smith
messages: + msg91779
resolution: not a bug
stage: resolved
2009-08-20 10:27:16mvyskocilcreate