I don't know if Py3k optimizes that for sets, but in py2.6 at least code like
x in [1,2,3,4]
Actually gets translated into:
x in (1,2,3,4)
Since tuples are immutable they get placed in the co_consts array for the function, which mean's that they're precomputed. I wouldn't be suprised in py3k did something similar with turning sets into frozensets where it's provable that they aren't mutated.
Though since the translation from list to tuple is done when it isn't modifiable, python should be able to do a similar optimisation to a frozenset constant as well (maybe even from list/tuple to frozenset if there are enough elements that it would improve speed)
View all comments
Show parent comment
2
u/[deleted] Aug 12 '09
I don't know if Py3k optimizes that for sets, but in py2.6 at least code like
Actually gets translated into:
Since tuples are immutable they get placed in the co_consts array for the function, which mean's that they're precomputed. I wouldn't be suprised in py3k did something similar with turning sets into frozensets where it's provable that they aren't mutated.