有很多次我认为生成器样式可以更直接地返回列表,例如,
def foo(input_array):
for x in input_array:
yield processed(x)
Run Code Online (Sandbox Code Playgroud)
与
def bar(input_array):
accumulator = []
for x in input_array:
accumulator.append(processed(x))
return accumulator
Run Code Online (Sandbox Code Playgroud)
(好吧,如果它真的那么简单,我会写map,但你明白了:生成器版本更清晰).但是,并不总是需要返回类型的发电机.是否有一个内置的装饰器,我可以用它来改变foo一个返回列表或元组的函数?我自己写的方式是,
import functools
def transform_return_value(transformer):
def inner(f):
@functools.wraps(f)
def new_f(*argv, **kwargs):
return transformer(f(*argv, **kwargs))
return new_f
return inner
@transform_return_value(list)
def foo(input_array):
for x in input_array:
yield processed(x)
Run Code Online (Sandbox Code Playgroud)
Dav*_*ver 24
据我所知(我看过,因为我想知道完全相同的事情),不:没有直接的方法来使用标准库.
listify但是在unstdlib.py库中有一个经过彻底测试的包装器:https://github.com/shazow/unstdlib.py/blob/master/unstdlib/standard/list_.py#L149
def listify(fn=None, wrapper=list):
"""
A decorator which wraps a function's return value in ``list(...)``.
Useful when an algorithm can be expressed more cleanly as a generator but
the function should return an list.
Example::
>>> @listify
... def get_lengths(iterable):
... for i in iterable:
... yield len(i)
>>> get_lengths(["spam", "eggs"])
[4, 4]
>>>
>>> @listify(wrapper=tuple)
... def get_lengths_tuple(iterable):
... for i in iterable:
... yield len(i)
>>> get_lengths_tuple(["foo", "bar"])
(3, 3)
"""
def listify_return(fn):
@wraps(fn)
def listify_helper(*args, **kw):
return wrapper(fn(*args, **kw))
return listify_helper
if fn is None:
return listify_return
return listify_return(fn)
Run Code Online (Sandbox Code Playgroud)
尽管@David Wolever 的回答确实是最干净的方法,但我经常发现自己做的一件事(因为它不需要定义外部装饰器)是将生成器编写为本地函数,如下所示:
def foo(input_array):
def gen():
for x in input_array:
yield processed(x)
return list(gen())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9208 次 |
| 最近记录: |