Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用函数和参数作为函数的参数-奇怪的行为?_Python_Python 3.x - Fatal编程技术网

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-是的,我完全同意。)