在原来python2里有一个函数apply来做这种事情,如下:
# File: builtin-apply-example-1.py
def function(a, b):
print a, b
apply(function, ("whither", "canada?"))
apply(function, (1, 2 + 3))
# File: builtin-apply-example-2.py
def function(a, b):
print a, b
apply(function, ("crunchy", "frog"))
apply(function, ("crunchy",), {"b": "frog"})
apply(function, (), {"a": "crunchy", "b": "frog"})
现在换成python3了,就不能这样使用了,因为这个函数已经不再使用了。那么怎么办呢?
可以如下调用:
# File: builtin-apply-example-1.py
def function(a, b):
print(a, b)
#apply(function, ("whither", "canada?"))
#apply(function, (1, 2 + 3))
function(*("whither", "canada?"))
function(*(1, 2 + 3))
# File: builtin-apply-example-1.py
def function(a, b):
print(a, b)
#apply(function, ("whither", "canada?"))
#apply(function, (1, 2 + 3))
function(*("whither", "canada?"))
function(*(1, 2 + 3))
function(**{"a": "crunchy", "b": "frog"})
可以看到采用*元组、**字典的方式调用。