返回生成器的生成器和函数之间的区别

时间:2016-09-10 14:22:11

标签: python generator

我正在使用生成器调试一些代码,并提出了这个问题。假设我有一个生成器函数

def f(x):
    yield x

和一个返回生成器的函数:

def g(x):
    return f(x)

他们肯定会回归同样的事情。在Python代码中交替使用它们会有什么不同吗?有没有办法区分这两者(没有inspect)?

4 个答案:

答案 0 :(得分:2)

检查它的最佳方法是使用inspect.isgeneratorfunction,这是一个非常简单的功能:

def ismethod(object):
    """Return true if the object is an instance method.

    Instance method objects provide these attributes:
        __doc__         documentation string
        __name__        name with which this method was defined
        im_class        class object in which this method belongs
        im_func         function object containing implementation of method
        im_self         instance to which this method is bound, or None"""
    return isinstance(object, types.MethodType)

def isfunction(object):
    """Return true if the object is a user-defined function.

    Function objects provide these attributes:
        __doc__         documentation string
        __name__        name with which this function was defined
        func_code       code object containing compiled function bytecode
        func_defaults   tuple of any default values for arguments
        func_doc        (same as __doc__)
        func_globals    global namespace in which this function was defined
        func_name       (same as __name__)"""
    return isinstance(object, types.FunctionType)

def isgeneratorfunction(object):
    """Return true if the object is a user-defined generator function.

    Generator function objects provides same attributes as functions.

    See help(isfunction) for attributes listing."""
    return bool((isfunction(object) or ismethod(object)) and
                object.func_code.co_flags & CO_GENERATOR)

现在,如果您使用如下语法声明您的生成器:

my_generator = (i*i for i in range(1000000))

在这种情况下,您可以非常轻松地检查其类型,例如,__class__将返回<type 'generator'>

答案 1 :(得分:1)

他们会采取同样的行动。以及如何区分两者(没有inspect)。在python中?只检查:

import inspect

print inspect.isgeneratorfunction(g) --> False
print inspect.isgeneratorfunction(f) --> True

当然,您也可以使用dis

进行检查
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(x):
...     yield x
... 
>>> def g(x):
...     return f(x)
... 
>>> import dis
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (x)
              3 YIELD_VALUE         
              4 POP_TOP             
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        
>>> dis.dis(g)
  2           0 LOAD_GLOBAL              0 (f)
              3 LOAD_FAST                0 (x)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE  

inspect更合适。

答案 2 :(得分:1)

If you want to identify what is a generator, the answer is simple. f is a generator because it contains the yield statement. g is not a generator because it does not contain the yield statement. (You can have a look at https://docs.python.org/2/reference/simple_stmts.html?highlight=yield#the-yield-statement

至于使用它们的区别是什么,它们完全相同。您可以将生成器存储在变量中,然后在for语句中使用它。在这种情况下,g(x)只是扮演一个中间人&#34;。看看以下示例:

def f(x):
  for r in range(x):
    yield r

def g(x):
  return f(x)

print "using f(x)"
for i in f(3):
  print i

print "using g(x)"
for j in g(3):
  print j

print "store the iterator f(x) in a variable, then use it in a 'for' statement"
m = f(3)
for k in m:
  print k

print "store the iterator f(x) returned by g(x), then use it in a 'for' statement"
n = g(3)
for k in n:
  print k

这些是在python2中。只需在python3的print语句中添加括号即可。

答案 3 :(得分:1)

我喜欢turkus回答,但是显示的示例主要是理论上的,并且在日常编码中并不常见。

生成器函数(使用yield)和返回生成器的函数之间的主要实际区别是生成器函数被延迟评估

考虑这个会议:

$ python
Python 3.6.0 
[GCC 6.3.1 20170109] on linux
>>> def a():
...     print('in a')
...     yield 0
... 
>>> def b():
...     print('in b')
...     return iter(range(1))
... 
>>> aa = a()
>>> next(aa)
in a
0
>>> next(aa)
Traceback ...
StopIteration
>>> bb = b()
in b
>>> next(bb)
0
>>> next(bb)
Traceback ...
StopIteration