Message256403
> Would option (1) work if wrap 1 and 1.0 in a tuple? In a list? In a custom collection?
Right now, the Python compiler is quite limited. It only produces constants for integers and strings, not complex types. The peephole optimizers is responsible to produce tuple and frozenset constants, and the optimizer is more naive. It doesn't try to merge constants, nor remove items from co_consts to only keep the new container. Example:
>>> def f():
... return (1,2,3)
...
>>> f.__code__.co_consts
(None, 1, 2, 3, (1, 2, 3))
1, 2, 3 constants are kept, whereas only (1, 2, 3) constant is used.
Test with my patch:
>>> f1, f2 = lambda x: x in {1,}, lambda x: x in {1.0,}
>>>
>>> f1.__code__ is f2.__code__
False
>>> f1.__code__.co_consts
(None, 1, frozenset({1}))
>>> f2.__code__.co_consts
(None, 1.0, frozenset({1.0}))
The frozenset are different are expected. |
|
Date |
User |
Action |
Args |
2015-12-14 17:07:43 | vstinner | set | recipients:
+ vstinner, arigo, rhettinger, ncoghlan, donmez, ezio.melotti, r.david.murray, torsten, BreamoreBoy, fijall, serhiy.storchaka, David MacIver, abarry, Kevin Shweh, Tijs Van Oevelen |
2015-12-14 17:07:43 | vstinner | set | messageid: <1450112863.76.0.162806899856.issue25843@psf.upfronthosting.co.za> |
2015-12-14 17:07:43 | vstinner | link | issue25843 messages |
2015-12-14 17:07:43 | vstinner | create | |
|