I have a specific situation in which I would like to do the following (actually it is more involved than this, but I reduced the problem to the essence):
>>> (lambda e: 1)(0) if (lambda e: True)(0) else (lambda e: 2)(0)
True
which is a difficult way of writing:
>>> 1 if True else 2
1
but in reality '1','True' and '2' are additional expressions that get evaluated and which require the variable 'e', which I set to '0' for this simplified code example.
Note the difference in output from both expressions above, although
>>> (lambda e: 1)(0)
1
>>> (lambda e: True)(0)
True
>>> (lambda e: 2)(0)
2
The funny thing is that this is a special case, because if I replace '1' by '3' I get the expected/desired result:
>>> (lambda e: 3)(0) if (lambda e: True)(0) else (lambda e: 2)(0)
3
It's even correct if I replace '1' by '0' (which could also be a special case since 1==True and 0==False)
>>> (lambda e: 0)(0) if (lambda e: True)(0) else (lambda e: 2)(0)
0
Also, if I replace 'True' by 'not False' or 'not not True', it still works:
>>> (lambda e: 1)(0) if (lambda e: not False)(0) else (lambda e: 2)(0)
1
>>> (lambda e: 1)(0) if (lambda e: not not True)(0) else (lambda e: 2)(0)
1
Another alternative formulation uses the usual if..then..else statement and does not produce the error:
>>> if (lambda e: True)(0):
(lambda e: 1)(0)
else:
(lambda e: 2)(0)
1
What explains this behavior? How can I solve this behavior in a nice way (avoid to use 'not not True' or something?
Thanks!
I think I figured out why the bug is happening, and why your repro is Python 3 specific.
Code objects do equality comparisons by value, rather than by pointer, strangely enough:
In Python 2,
lambda e: True
does a global name lookup andlambda e: 1
loads a constant1
, so the code objects for these functions don't compare equal. In Python 3,True
is a keyword and both lambdas load constants. Since1 == True
, the code objects are sufficiently similar that all the checks incode_richcompare
pass, and the code objects compare the same. (One of the checks is for line number, so the bug only appears when the lambdas are on the same line.)The bytecode compiler calls
ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts)
to create theLOAD_CONST
instruction that loads a lambda's code onto the stack, andADDOP_O
uses a dict to keep track of objects it's added, in an attempt to save space on stuff like duplicate constants. It has some handling to distinguish things like0.0
,0
, and-0.0
that would otherwise compare equal, but it wasn't expected that they'd ever need to handle equal-but-inequivalent code objects. The code objects aren't distinguished properly, and the two lambdas end up sharing a single code object.By replacing
True
with1.0
, we can reproduce the bug on Python 2:I don't have Python 3.5, so I can't check whether the bug is still present in that version. I didn't see anything in the bug tracker about the bug, but I could have just missed the report. If the bug is still there and hasn't been reported, it should be reported.