延迟功能的拆箱结果 [英] Unpacking result of delayed function

查看:61
本文介绍了延迟功能的拆箱结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用延迟转换程序的同时,我偶然发现了一种常用的编程模式,该模式不适用于延迟。示例:

While converting my program using delayed, I stumbled upon a commonly used programming pattern that doesn't work with delayed. Example:

from dask import delayed
@delayed
def myFunction():
    return 1,2

a, b = myFunction()
a.compute()

引发: TypeError:未指定长度的延迟对象是不可迭代的
而以下解决方法则不能。但是看起来很笨拙

Raises: TypeError: Delayed objects of unspecified length are not iterable While the following work around does not. But looks a lot more clumsy

from dask import delayed
@delayed
def myFunction():
    return 1,2

dummy = myFunction()
a, b = dummy[0], dummy[1]
a.compute()

这是预期的行为吗?

推荐答案

使用 nout = 关键字,如延迟的文档字符串

@delayed(nout=2)
def f(...):
    return a, b

x, y = f(1)



文档字符串



Docstring

nout : int, optional
    The number of outputs returned from calling the resulting ``Delayed``
    object. If provided, the ``Delayed`` output of the call can be iterated
    into ``nout`` objects, allowing for unpacking of results. By default
    iteration over ``Delayed`` objects will error. Note, that ``nout=1``
    expects ``obj``, to return a tuple of length 1, and consequently for
    `nout=0``, ``obj`` should return an empty tuple.

这篇关于延迟功能的拆箱结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆