小编Gae*_*tan的帖子

Python可以优化这个简单的变量调用吗?

似乎python(2.6)无法优化这个简单的临时变量'a'?

我用来为一些代码分配一个局部变量,以减少行长度.

对我来说,这是一个简单的优化任何正确的编译器可以自动执行

from dis import dis

def f(func):
  func()

def functioncall():
  print ' => function called'

def unoptimized():
  print 'in unoptimized'
  a = functioncall
  f(func=a)

def optimized():
  print 'in optimized'
  f(func=functioncall)

unoptimized()
optimized()

print 'dis(unoptimized)'
dis(unoptimized)
print 'dis(optimized)'
dis(optimized)
Run Code Online (Sandbox Code Playgroud)

输出:

in unoptimized
 => function called
in optimized
 => function called
dis(unoptimized)
 10           0 LOAD_CONST               1 ('in unoptimized')
              3 PRINT_ITEM
              4 PRINT_NEWLINE

 11           5 LOAD_GLOBAL              0 (functioncall)
              8 STORE_FAST               0 (a)

 12          11 LOAD_GLOBAL              1 (f)
             14 LOAD_CONST               2 ('func') …
Run Code Online (Sandbox Code Playgroud)

python python-2.6

3
推荐指数
1
解决办法
202
查看次数

标签 统计

python ×1

python-2.6 ×1