Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 为什么这个函数总是产生一个生成器?_Function_Python 3.x_Generator_Python 3.3 - Fatal编程技术网

Function 为什么这个函数总是产生一个生成器?

Function 为什么这个函数总是产生一个生成器?,function,python-3.x,generator,python-3.3,Function,Python 3.x,Generator,Python 3.3,我对Python 3.3中的以下内容感到困惑: >>> def foo(gen=False): ... if not gen: ... return list(range(10)) ... else: ... for i in range(10): ... yield i ... >>> foo() <generator object foo at 0xb72a016c> &

我对Python 3.3中的以下内容感到困惑:

>>> def foo(gen=False):
...     if not gen:
...         return list(range(10))
...     else:
...         for i in range(10):
...             yield i
... 
>>> foo()
<generator object foo at 0xb72a016c>
>>> foo(gen=False)
<generator object foo at 0xb72a0144>
>>> foo(gen=True)
<generator object foo at 0xb72a089c>
>>> 
def foo(gen=False): ... 如果不是gen: ... 返回列表(范围(10)) ... 其他: ... 对于范围(10)内的i: ... 产量一 ... >>>foo() >>>foo(gen=False) >>>foo(gen=True) >>>
我误解了什么?如果
gen
为False,那么
not gen
的默认值为
True
,因此我应该得到一个整数列表
[0,1,2,3,4,5,6,7,8,9]
。另一方面,如果它是
真的
,那么
(而不是gen)=False不应该导致生成器吗?

在函数中包含
收益
,使其成为生成器函数:当你执行函数时,你得到一个生成器;没有其他执行发生。只有当生成器开始被请求元素时,函数本身才会开始执行

def not_a_generator():
    print(1)
    print(2)

not_a_generator()
# => 1
#    2

def is_a_generator():
    print(1)
    yield 7
    print(2)

is_a_generator()
# => <generator object bar at 0x10e1471a8>

list(is_a_generator())
# => 1
#    2
#    [7]
def not_a_生成器():
印刷品(1)
印刷品(2)
不是一个发电机()
# => 1
#    2
def是一个生成器():
印刷品(1)
收益率7
印刷品(2)
是发电机吗
# => 
列表(是一个生成器())
# => 1
#    2
#    [7]

在函数中包含
收益
使其成为生成器函数:当您执行函数时,您得到一个生成器;没有其他执行发生。只有当生成器开始被请求元素时,函数本身才会开始执行

def not_a_generator():
    print(1)
    print(2)

not_a_generator()
# => 1
#    2

def is_a_generator():
    print(1)
    yield 7
    print(2)

is_a_generator()
# => <generator object bar at 0x10e1471a8>

list(is_a_generator())
# => 1
#    2
#    [7]
def not_a_生成器():
印刷品(1)
印刷品(2)
不是一个发电机()
# => 1
#    2
def是一个生成器():
印刷品(1)
收益率7
印刷品(2)
是发电机吗
# => 
列表(是一个生成器())
# => 1
#    2
#    [7]

yield
语句放在
if
分支中并不重要。报告说:

在函数定义中使用yield足以使该定义创建一个生成器函数而不是普通函数

但是,只需定义一个内部生成器函数,即可实现预期目标:

>>> def foo(gen=False):
...     if not gen:
...         return list(range(10))
...     else:
...         def foo():                 # the inner generator
...             for i in range(10):
...                 yield i
...         return foo()

>>> foo()    
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> foo(gen=False)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> foo(gen=True)
<generator object foo.<locals>.foo at 0x7f350c7563b8>
>>> g = foo(gen=True)
>>> next(g)
0
def foo(gen=False): ... 如果不是gen: ... 返回列表(范围(10)) ... 其他: ... def foo():#内部生成器 ... 对于范围(10)内的i: ... 产量一 ... 返回foo() >>>foo() [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>foo(gen=False) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>foo(gen=True) >>>g=foo(gen=True) >>>下一个(g) 0

这一次,
yield
语句将内部的
foo
转换为生成器。外部
foo
仍然是一个正常函数。

yield
语句放在
if
分支中并不重要。报告说:

在函数定义中使用yield足以使该定义创建一个生成器函数而不是普通函数

但是,只需定义一个内部生成器函数,即可实现预期目标:

>>> def foo(gen=False):
...     if not gen:
...         return list(range(10))
...     else:
...         def foo():                 # the inner generator
...             for i in range(10):
...                 yield i
...         return foo()

>>> foo()    
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> foo(gen=False)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> foo(gen=True)
<generator object foo.<locals>.foo at 0x7f350c7563b8>
>>> g = foo(gen=True)
>>> next(g)
0
def foo(gen=False): ... 如果不是gen: ... 返回列表(范围(10)) ... 其他: ... def foo():#内部生成器 ... 对于范围(10)内的i: ... 产量一 ... 返回foo() >>>foo() [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>foo(gen=False) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>foo(gen=True) >>>g=foo(gen=True) >>>下一个(g) 0

这一次,
yield
语句将内部的
foo
转换为生成器。外部的
foo
仍然是一个正常的函数。

因为如果函数中的任何地方都有
yield
,python将始终为您提供一个生成器,即使它从来都不是reached@inspectorG4dget哇,我不知道。现在这似乎是一个愚蠢的问题,但我从未在任何地方读过。的可能重复,因为如果函数中的任何地方都有
yield
,python将始终为您提供一个生成器,即使它从来没有reached@inspectorG4dget哇,我不知道。这似乎是一个愚蠢的问题,但我从未在任何地方读过。可能的重复我理解这一切是如何机械地运作的,正如你所写的,我只是没有意识到你不能用我在上面的答案中写的方式来做(也就是说,通过@inspectorG4dget的评论,这个关键字在控制流中的位置并不重要。它自动是一个生成器函数P:)。不过,我很感激你的回答:)我理解这一切是如何机械地工作的,就像你写的一样,我只是没有意识到你不能用我在上面的回答中写的方式来做(也就是说,通过@inspectorG4dget的评论,该关键字在控制流中的位置等并不重要。它会自动生成一个生成器函数P:)。不过,我感谢您的回答:)