I've inspected .__code__ objects for two functions I deemed different, but found to be identical, for a variety of expressions. If code objects are identical, as far as I understand, they compile to same bytecode, and are thus "same" functions.
Table below is of things inserted before ; pass that makes g have different __code__. Since f is a "do nothing" function, this suggests everything under "same" to never execute, including the long arithmetic. Further, a tuple is "same", but a list and a string are "diff" - so we might conclude that unassigned expressions involving immutable literals aren't evaluated. But then there's 1/0, which might be an "exception" due to raising an Exception - then what of 10**99 vs. 10**9? 10**99 doesn't raise an Exception and can be assigned.
I couldn't tell much from profiling; both "same" and "diff" had indistinguishable execution times. When they could be distinguished, however, it was always with "diff".
If "same"s never execute, then how does Python determine what to or not to execute? If they do execute, how are their code objects same?
Same:
0,(0,),True,False,None10 ** 9()-314159.265358 ** (1/12345) / 2.718281828 + 500 - 7j
Diff:
[0],{0: 0}10 ** 99[],{},""
Comparison code:
def compare(fn1, fn2):
for name in dir(fn1.__code__):
if (name.startswith("co_") and
name not in ("co_filename", "co_name", "co_firstlineno")):
v1 = getattr(fn1.__code__, name)
v2 = getattr(fn2.__code__, name)
if v1 == v2:
print(name.ljust(18), "same")
else:
print(name.ljust(18), "diff", v1, v2)
def f():
pass
def g():
10 ** 99; pass
The following differ: co_name (always), co_filename (IPython), co_firstlineno (from file) - but don't affect what's "executed", correct me if wrong; from docs, co_code is what should differ.
Note: accepted answer misses an important piece of intuition: unassigned literals code may be kept if code required to store the value takes more memory than code required to store the expression to compute the value; that's the case with 10 ** 99 (at least, that's what was asserted in the comments). See comments below the answer for further info.