Python为什么不优化掉临时变量? [英] Why doesn't Python optimize away temporary variables?

查看:140
本文介绍了Python为什么不优化掉临时变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fowler的提取变量重构方法,以前是其他语言优化了临时变量,因此不会浪费时间或空间资源. Python为什么不这样做?

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

推荐答案

因为Python是一种高度动态的语言,所以引用可以影响行为.

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

例如,比较以下内容:

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

如果Python将"ob1"和"ob2"变量优化"掉了,行为就会改变.

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

Python对象的生存期由引用计数控制.将弱引用添加到混合加线程中,您将看到优化的余地变量(甚至是局部变量)也会导致不良的行为变化.

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.

此外,在Python中,从性能的角度来看,删除这些变量几乎不会改变任何内容.本地名称空间已经高度优化(通过数组中的索引查找值);如果您担心取消引用局部变量的速度,则在项目的那个时间关键部分使用了错误的编程语言.

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.

这篇关于Python为什么不优化掉临时变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆