This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients BreamoreBoy, David MacIver, Kevin Shweh, Tijs Van Oevelen, abarry, arigo, donmez, ezio.melotti, fijall, ncoghlan, r.david.murray, rhettinger, serhiy.storchaka, torsten, vstinner
Date 2015-12-14.17:07:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1450112863.76.0.162806899856.issue25843@psf.upfronthosting.co.za>
In-reply-to
Content
> 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.
History
Date User Action Args
2015-12-14 17:07:43vstinnersetrecipients: + 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:43vstinnersetmessageid: <1450112863.76.0.162806899856.issue25843@psf.upfronthosting.co.za>
2015-12-14 17:07:43vstinnerlinkissue25843 messages
2015-12-14 17:07:43vstinnercreate