Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Python optimize away temporary variables?

Fowler's Extract Variable refactoring method, formerly Introduce Explaining Variable, says use a temporary variable to make code clearer for humans. The idea is to elucidate complex code by introducing an otherwise unneeded local variable, and naming that variable for exposition purposes. It also advocates this kind of explaining over comments.. Other languages optimize away temporary variables so there's no cost in time or space resources. Why doesn't Python do this?

In [3]: def multiple_of_six_fat(n):
   ...:     multiple_of_two = n%2 == 0
   ...:     multiple_of_three = n%3 == 0
   ...:     return multiple_of_two and multiple_of_three
   ...:

In [4]: def multiple_of_six_lean(n):
   ...:     return n%2 == 0 and n%3 == 0
   ...:
In [5]: import dis

In [6]: dis.dis(multiple_of_six_fat)
  2           0 LOAD_FAST                0 (n)
              3 LOAD_CONST               1 (2)
              6 BINARY_MODULO
              7 LOAD_CONST               2 (0)
             10 COMPARE_OP               2 (==)
             13 STORE_FAST               1 (multiple_of_two)

  3          16 LOAD_FAST                0 (n)
             19 LOAD_CONST               3 (3)
             22 BINARY_MODULO
             23 LOAD_CONST               2 (0)
             26 COMPARE_OP               2 (==)
             29 STORE_FAST               2 (multiple_of_three)

  4          32 LOAD_FAST                1 (multiple_of_two)
             35 JUMP_IF_FALSE_OR_POP    41
             38 LOAD_FAST                2 (multiple_of_three)
        >>   41 RETURN_VALUE

In [7]: dis.dis(multiple_of_six_lean)
  2           0 LOAD_FAST                0 (n)
              3 LOAD_CONST               1 (2)
              6 BINARY_MODULO
              7 LOAD_CONST               2 (0)
             10 COMPARE_OP               2 (==)
             13 JUMP_IF_FALSE_OR_POP    29
             16 LOAD_FAST                0 (n)
             19 LOAD_CONST               3 (3)
             22 BINARY_MODULO
             23 LOAD_CONST               2 (0)
             26 COMPARE_OP               2 (==)
        >>   29 RETURN_VALUE
like image 517
Bob Stein Avatar asked Dec 11 '22 00:12

Bob Stein


1 Answers

Because Python is a highly dynamic language, and references can influence behaviour.

Compare the following, for example:

>>> id(object()) == id(object())
True
>>> ob1 = object()
>>> ob2 = object()
>>> id(ob1) == id(ob2)
False

Had Python 'optimised' the ob1 and ob2 variables away, behaviour would have changed.

Python object lifetime is governed by reference counts. Add weak references into the mix plus threading, and you'll see that optimising away variables (even local ones) can lead to undesirable behaviour changes.

Besides, in Python, removing those variables would hardly have changed anything from a performance perspective. The local namespace is already highly optimised (values are looked up by index in an array); if you are worried about the speed of dereferencing local variables, you are using the wrong programming language for that time critical section of your project.

like image 190
Martijn Pieters Avatar answered Dec 13 '22 12:12

Martijn Pieters