Python 使用函数和参数作为函数的参数-奇怪的行为?
这个问题应该很容易回答,但是我什么也没找到/不知道如何搜索Python 使用函数和参数作为函数的参数-奇怪的行为?,python,python-3.x,Python,Python 3.x,这个问题应该很容易回答,但是我什么也没找到/不知道如何搜索 def testcall(function, argument): function(*argument) testcall(print, "test") # output: t e s t 为什么使用(*参数)将参数分解为单个字符。然后,您将这些字符传递给打印。这与做以下事情没有什么不同: >>> print('t', 'e', 's', 't') t e s t >>> 删除*以修
def testcall(function, argument):
function(*argument)
testcall(print, "test")
# output:
t e s t
为什么使用(*参数
)将参数
分解为单个字符。然后,您将这些字符传递给打印
。这与做以下事情没有什么不同:
>>> print('t', 'e', 's', 't')
t e s t
>>>
删除
*
以修复问题:
>>> def testcall(function, argument):
... function(argument)
...
>>> testcall(print, "test")
test
>>>
你的脾脏是不对称的。应该是:
def testcall(function, *argument):
function(*argument)
通常,如果希望函数的行为与另一个函数类似,它应该接受splat并发送splat
*参数
将字符串拆分为4个字符的数组,因此调用print't',e',s',t'
;您应该将参数
声明为*参数
ok,但是我如何使用此函数调用具有2个参数的函数呢?谢谢!回答得很好,但是,我认为“Cuadue搞定了,对吗?”@speendo-是的,我完全同意。)