关于Python在生成器表达式中的内部到外部评估顺序

时间:2013-01-18 09:39:24

标签: python generator operator-precedence

迭代以下生成器表达式时,

fun(i) for i in mylist if i not in setA.union(setB)

是每次迭代中调用的setA.union方法,还是只调用一次?

3 个答案:

答案 0 :(得分:3)

是的,每次在迭代中调用setA.union(setB)

你应该在生成器表达式之前执行此操作并将其存储在变量

setvalues = setA.union(setB)

fun(i) for i in mylist if i not in setvalues

答案 1 :(得分:3)

使用dis模块进行简单证明:

In [24]: def func():
   ....:     a=set([1,2,3])
   ....:     b=set([3,4,5])
   ....:     c=[i for i in xrange(10) if i in a.union(b)]
   ....:     

In [25]: dis.dis(func)

  4          42 BUILD_LIST               0
             45 LOAD_GLOBAL              1 (xrange)
             48 LOAD_CONST               6 (10)
             51 CALL_FUNCTION            1
             54 GET_ITER                          #iterator returned from xrange

        >>   55 FOR_ITER                33 (to 91) #until the iterator is not exhausted
             58 STORE_FAST               2 (i)
             61 LOAD_FAST                2 (i)
             64 LOAD_FAST                0 (a)
             67 LOAD_ATTR                2 (union)
             70 LOAD_FAST                1 (b)
             73 CALL_FUNCTION            1      #union() is being called in each iteration
             76 COMPARE_OP               6 (in)
             79 POP_JUMP_IF_FALSE       55
             82 LOAD_FAST                2 (i)
             85 LIST_APPEND              2
             88 JUMP_ABSOLUTE           55
                                                         #end of loop    
        >>   91 STORE_FAST               3 (c)
             94 LOAD_CONST               0 (None)
             97 RETURN_VALUE 

因此,对于您的示例,将在每次迭代中调用它,即len(mylist)次。

答案 2 :(得分:3)

验证它的另一种方法是对其进行分析。借用@AshwiniChaudhary的例子,我将通过cProfile运行它

>>> def func():
     a=set([1,2,3])
     b=set([3,4,5])
     c=[i for i in xrange(10) if i in a.union(b)]


>>> import cProfile
>>> cProfile.run("func()")
         13 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <pyshell#1045>:1(func)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
       10    0.000    0.000    0.000    0.000 {method 'union' of 'set' objects}

这里明确提到,联盟被称为10