简体   繁体   English

使用返回函数的变量作为另一个函数的参数

[英]Using variables from a returned function as arguments in another function

I have a function that has 4 parameters defined as: 我有一个函数,具有4个参数定义为:

call(a,b,c,d):

I have another function called 我有另一个功能叫做

return_args() #that returns 4 variables. 

When I do the following: 当我执行以下操作时:

call(return_args()) #it errors out saying, that's 1 arguments, not 4 arguments 

I know I can store the 4 variables in another line previous to the call(), but it it possible to return the variables all within call() line. 我知道我可以将4个变量存储在call()之前的另一行中,但是有可能将所有变量都返回call()行中。

You can use * argument unpacking 您可以使用*参数解包

call(*return_args())

This is a shorthand form of the following: 这是以下内容的简写形式:

a, b, c, d = return_args()
call(a, b, c, d)

or even 甚至

tup = return_args()
call(*tup)

Python's * operator used in this way essentially says "unpack this tuple and pass the unpacked values to this function as arguments". 这种方式使用的Python *运算符本质上说“解压缩该元组并将解压缩的值作为参数传递给此函数”。

The related operator ** does a similar trick for keyword arguments: 相关运算符**对关键字参数也有类似的技巧:

def call(arg1=None, arg2=None, **kwargs):
    pass

kwargs = {'arg1': 'test', 'arg3': 'whatever'}
call(**kwargs)

* and ** can be used together: ***可以一起使用:

def call(*args, **kwargs):
    pass
call(*return_args(), **kwargs)

您可以使用*运算符将列表作为单独的参数发送:

call(*return_args())

Use the 'variable unpacking' syntax: call( *return_args() ) 使用“变量拆包”语法: call( *return_args() )

Alternatively you can use an obsolete syntax: apply( call, return_args() ) 或者,您可以使用过时的语法: apply( call, return_args() )

return_args() #that returns 4 variables. 

This is a misunderstanding, it doesn't return 4 variables. 这是一个误解,它不会返回4个变量。 A function like: 类似的功能:

def return_args():
    return 1, 2, 3, 4

Is actually doing this: 实际上是这样做的:

def return_args():
    mytuple = (1, 2, 3, 4)
    return mytuple

and returning a single thing. 并返回一件东西。

Another side of this is "destructuring assignment" in Python, which is the ability to do this: 另一方面是Python中的“销毁分配”功能,可以执行以下操作:

a, b = 1, 2

It's a way to assign/bind two variables at once, but it's actually creating then unpacking the sequence (1,2) . 这是一次分配/绑定两个变量的方法,但实际上是在创建然后解压缩序列(1,2) You can write: 你可以写:

a, b, c, d = return_args()

and it looks like you returned four things and bound them to four variable names and that's a clean, useful abstraction, but that's not what happened - actually one sequence was created (with 4 things in it), then it was unpacked to match a sequence of variable names. 看起来您返回了四件事,并将它们绑定到四个变量名,这是一个干净,有用的抽象,但这不是发生的事情—实际上创建了一个序列(其中包含四个东西),然后将其解压缩以匹配一个序列变量名。

The two abstractions leak, and you find out that return_args() is returning a single thing when you try to do this: 这两个抽象泄漏,当您尝试执行以下操作时,您发现return_args()返回的是单个内容:

call(return_args()) #it errors out saying, that's 1 arguments, not 4 arguments

The other answers are rightly suggesting call(*return_args()) as one solution, it's documented here under "Unpacking argument lists": http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists 其他答案正确地建议将call(*return_args())作为一种解决方案,在“解压缩参数列表”下记录在这里: http : //docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists

(The other side to this is a function created to accept variable numbers of arguments discussed here: https://stackoverflow.com/a/11550319/478656 ) (另一端是创建的函数,用于接受此处讨论的可变数量的参数: https : //stackoverflow.com/a/11550319/478656

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM