一个 Python 函数可以同时是生成器和“非生成器”吗?

huangapple go评论72阅读模式
英文:

Can a python function be both a generator and a "non-generator"?

问题

我有一个函数我想要从中生成字节生成器行为),并根据`save`布尔值是否设置来写入文件非生成器行为)。这是否可能

```python
def encode_file(source, save=False, destination=None):
    # 每次处理输入文件的内容3字节
    print('hello')
    with open(source, 'rb') as infile:
        # 将字节保存到目标文件
        if save:
            print(f'saving to file {destination}')
            with open(destination, 'wb') as outfile:
                while (bytes_to_encode := infile.read(3)):
                    l = len(bytes_to_encode)
                    if l < 3:
                        bytes_to_encode += (b'\x00' * (3 - l))
                    outfile.write(bytes_to_encode)
            return
        # 向调用者生成字节
        else:
            while (bytes_to_encode := infile.read(3)):
                l = len(bytes_to_encode)
                if l < 3:
                    bytes_to_encode += (b'\x00' * (3 - l)) # 如果不足3字节则填充位
                yield encode(bytes_to_encode)
            return

在上面的实现中,该函数始终表现为生成器。当我调用

encode_file('file.bin', save=True, destination='output.base64')

它不会打印“hello”,而是返回一个生成器对象。这对我来说没有意义。不应该打印“hello”,然后将控制传递到代码的if save:部分,从而避免完全生成字节的部分吗?


<details>
<summary>英文:</summary>

I have a function which I want to yield bytes from (generator behaviour) and also write to a file (non-generator behaviour) depending on whether the `save` boolean is set. Is that possible?

```python
def encode_file(source, save=False, destination=None):
    # encode the contents of an input file 3 bytes at a time
    print(&#39;hello&#39;)
    with open(source, &#39;rb&#39;) as infile:
        # save bytes to destination file
        if save:
            print(f&#39;saving to file {destination}&#39;)
            with open(destination, &#39;wb&#39;) as outfile:
                while (bytes_to_encode := infile.read(3)):
                    l = len(bytes_to_encode)
                    if l &lt; 3:
                        bytes_to_encode += (b&#39;\x00&#39; * (3 - l))
                    outfile.write(bytes_to_encode)
            return
        # yield bytes to caller
        else:
            while (bytes_to_encode := infile.read(3)):
                l = len(bytes_to_encode)
                if l &lt; 3:
                    bytes_to_encode += (b&#39;\x00&#39; * (3 - l)) # pad bits if short
                yield encode(bytes_to_encode)
            return

In the above implementation, the function always behaves as a generator. When I call

encode_file(&#39;file.bin&#39;, save=True, destination=&#39;output.base64&#39;)

it does not print "hello" instead, it returns a generator object. This does not make sense to me. Shouldn't "hello" be printed and then shouldn't control be directed to the if save: portion of the code thus avoiding the part of the function that yields completely?

答案1

得分: 3

一个函数既不能是生成器,也不能不是生成器,但当然你可以通过定义一个辅助函数来决定是否返回一个生成器对象。为了避免在两者之间重复(读取)使用with(并减少冗余),将一个分支作为另一个的客户端:

def encode_file(source, save=False, destination=None):
    # 每次处理输入文件的内容3字节
    print('hello')
    # 将字节保存到目标文件
    if save:
        print(f'保存到文件 {destination}')
        with open(destination, 'wb') as outfile:
            for bytes_to_encode in encode_file(source):
                outfile.write(bytes_to_encode)
    # 将字节提供给调用者
    else:
        def g():
            with open(source, 'rb') as infile:
                while (bytes_to_encode := infile.read(3)):
                    l = len(bytes_to_encode)
                    if l < 3:
                        bytes_to_encode += (b'\x00' * (3 - l)) # 如果字节不足,填充位
                    yield encode(bytes_to_encode)
        return g()

(感谢interjay指出了在g中需要使用with的需要。)

英文:

A function can’t be a generator and also not be one, but of course you can decide whether to return a generator object or not by defining a helper function. To avoid duplicating the (read) with between the two (and reduce redundancy in general), make one branch a client of the other:

def encode_file(source, save=False, destination=None):
    # encode the contents of an input file 3 bytes at a time
    print(&#39;hello&#39;)
    # save bytes to destination file
    if save:
        print(f&#39;saving to file {destination}&#39;)
        with open(destination, &#39;wb&#39;) as outfile:
            for bytes_to_encode in encode_file(source):
                outfile.write(bytes_to_encode)
    # yield bytes to caller
    else:
        def g():
            with open(source, &#39;rb&#39;) as infile:
                while (bytes_to_encode := infile.read(3)):
                    l = len(bytes_to_encode)
                    if l &lt; 3:
                        bytes_to_encode += (b&#39;\x00&#39; * (3 - l)) # pad bits if short
                    yield encode(bytes_to_encode)
        return g()

(Thanks to interjay for pointing out the need for the with in g.)

答案2

得分: 2

你不能即时创建一个函数生成器,但你可以将逻辑提取到一个生成器函数中,然后创建一个单独的函数,该函数要么返回该生成器,要么将其保存到文件中。以下是一个用于演示目的的简单示例,带有注释:

# 作为生成器的示例函数
# 在这里放入所有的逻辑
def getNums_gen(n):
    for i in range(n):
        yield i


def getNums(n, asList=False):
    gen = getNums_gen(n)
    # 如果不想保存它,只需返回生成器
    if not asList:
        return gen

    # 如果想保存它,循环遍历生成器并执行保存数据所需的任何操作。
    # 在此示例中,我们将其保存为列表,但也可以写入文件
    lst = [num for num in gen]
    return lst


print(getNums(5, asList=True))
print(getNums(5, asList=False))
英文:

You can't make a function generator on the fly, but you can extract the logic into a generator function and then create a separate function that will either return that generator or save it to a file. Here's a simpler example for demonstration purposess with comments:

# example function as a generator
# put all your logic here
def getNums_gen(n):
    for i in range(n):
        yield i


def getNums(n, asList=False):
    gen = getNums_gen(n)
    # if we don&#39;t want to save it, just return the generator
    if not asList:
        return gen

    # if we want to save it, loop through the generator and do whatever
    # is needed to save the data. in this example, we&#39;ll just save it as a
    # list, but it could be written to a file too
    lst = [num for num in gen]
    return lst


print(getNums(5, asList=True))
print(getNums(5, asList=False))

huangapple
  • 本文由 发表于 2023年5月28日 23:42:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定