3 ms·
Yep yep, and only when no operations are done to it, otherwise it will end up storing the same constant multiple times and referring to each separately. For exa
by squeaky-clean 9y ago
Yep yep, and only when no operations are done to it, otherwise it will end up storing the same constant multiple times and referring to each separately. For example
>>> def f():
... x = 2221+1; y=2221+1
... return x is y
>>> f()
False
>>> f.__code__.co_consts
(None, 2221, 1, 2222, 2222)
>>> dis.dis(f)
2 0 LOAD_CONST 3 (2222)
2 STORE_FAST 0 (x)
4 LOAD_CONST 4 (2222)
6 STORE_FAST 1 (y)
3 8 LOAD_FAST 0 (x)
10 LOAD_FAST 1 (y)
12 COMPARE_OP 8 (is)
14 RETURN_VALUE
Not too sure when stuff like this would be useful (it goes way beyond the 'avoid using is unless checking references` advice), but it sure is fun.