很抱歉,您没有提供需要改写的句子。请提供您希望改写的句子,我将为您改写为一个长尾词的。
- 内容介绍
- 文章标签
- 相关推荐
本文共计403个文字,预计阅读时间需要2分钟。
在调用函数时,使用任意数量的参数时,最少的选项是默认参数。这些参数包括在元组或列表中的元素(详见元组和列表)。在可变数量的参数之前,可能有一个普通参数:def write_multiple_items(file):
调用函数时,使用任意数量的实参是最少见的选项。这些实参包含在元组中(详见元组和序列)。在可变数量的实参之前,可能有若干个普通参数:
def write_multiple_items(file, separator, *args):file.write(separator.join(args))def concat(*args, sep="/"):
return sep.join(args)
concat("earth", "mars", "venus")
'earth/mars/venus'
concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'
解包实参列表
函数调用要求独立的位置参数,但实参在列表或元组里时,要执行相反的操作。例如,内置的range()函数要求独立的start和stop实参。如果这些参数不是独立的,则要在调用函数时,用*操作符把实参从列表或元组解包出来:
list(range(3, 6)) # normal call with separate arguments[3, 4, 5]
args = [3, 6]
list(range(*args)) # call with arguments unpacked from a list
[3, 4, 5]
同样,字典可以用**操作符传递关键字参数:
def parrot(voltage, state='a stiff', action='voom'):print("-- This parrot wouldn't", action, end=' ')
print("if you put", voltage, "volts through it.", end=' ')
print("E's", state, "!")
d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
本文共计403个文字,预计阅读时间需要2分钟。
在调用函数时,使用任意数量的参数时,最少的选项是默认参数。这些参数包括在元组或列表中的元素(详见元组和列表)。在可变数量的参数之前,可能有一个普通参数:def write_multiple_items(file):
调用函数时,使用任意数量的实参是最少见的选项。这些实参包含在元组中(详见元组和序列)。在可变数量的实参之前,可能有若干个普通参数:
def write_multiple_items(file, separator, *args):file.write(separator.join(args))def concat(*args, sep="/"):
return sep.join(args)
concat("earth", "mars", "venus")
'earth/mars/venus'
concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'
解包实参列表
函数调用要求独立的位置参数,但实参在列表或元组里时,要执行相反的操作。例如,内置的range()函数要求独立的start和stop实参。如果这些参数不是独立的,则要在调用函数时,用*操作符把实参从列表或元组解包出来:
list(range(3, 6)) # normal call with separate arguments[3, 4, 5]
args = [3, 6]
list(range(*args)) # call with arguments unpacked from a list
[3, 4, 5]
同样,字典可以用**操作符传递关键字参数:
def parrot(voltage, state='a stiff', action='voom'):print("-- This parrot wouldn't", action, end=' ')
print("if you put", voltage, "volts through it.", end=' ')
print("E's", state, "!")
d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !

