python3中的可选yield或return.如何?

2023-09-02Python开发问题
8

本文介绍了python3中的可选yield或return.如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想要一个可以选择返回或产生结果的函数.这是一个例子.

I would like to have a function that can, optionally, return or yield the result. Here is an example.

def f(option=True):
    ...
    for...:
        if option:
            yield result
        else:
            results.append(result)

    if not option:
        return results

当然,这是行不通的,我用 python3 试过了,不管我设置什么选项值,我总是得到一个生成器.据我了解,python 检查函数的主体,如果存在 yield,则结果将是生成器.有什么办法可以解决这个问题并制作一个可以随意返回或屈服的函数?

Of course, this doesn't work, I have tried with python3 and I always get a generator no matter what option value I set. As far I have understood, python checks the body of the function and if a yield is present, then the result will be a generator. Is there any way to get around this and make a function that can return or yield at will?

推荐答案

你不能.任何 yield 的使用都会使函数成为生成器.

You can't. Any use of yield makes the function a generator.

您可以使用一个使用 list() 将生成器生成的所有值存储在列表对象中并返回的函数来包装您的函数:

You could wrap your function with one that uses list() to store all values the generator produces in a list object and returns that:

def f_wrapper(option=True):
    gen = f()
    if option:
        return gen    # return the generator unchanged
    return list(gen)  # return all values of the generator as a list

但是,一般来说,这是糟糕的设计.不要让你的函数像这样改变行为;坚持一种返回类型(生成器一个对象),不要让它在两者之间切换.

However, generally speaking, this is bad design. Don't have your functions alter behaviour like this; stick to one return type (a generator or an object) and don't have it switch between the two.

考虑将其拆分为两个函数:

Consider splitting this into two functions instead:

def f():
    yield result

def f_as_list():
    return list(f())

如果您需要生成器,请使用 f(),如果您想要列表,请使用 f_as_list().

and use either f() if you need the generator, and f_as_list() if you want to have a list instead.

由于 list() (和 next() 仅访问生成器的一个值)是内置函数,因此您很少需要使用包装器.直接调用这些函数即可:

Since list(), (and next() to access just one value of a generator) are built-in functions, you rarely need to use a wrapper. Just call those functions directly:

# access elements one by one
gen = f()
one_value = next(gen)

# convert the generator to a list
all_values = list(f())

这篇关于python3中的可选yield或return.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11