任意数量的参数, 这些参数将被收集为一个元组
def my_function(*args):
for arg in args:
print(arg)
my_function(1, 2, 3, 4)
# 输出:
# 1
# 2
# 3
# 4
允许函数接受任意数量的关键字参数。这些参数将被收集为一个字典
def my_function(**kwargs):
for key, value in kwargs.items():
print(f": ")
my_function(name='Alice', age=30, city='New York')
# 输出:
# name: Alice
# age: 30
# city: New York
def my_function(*args, **kwargs):
for arg in args:
print(arg)
for key, value in kwargs.items():
print(f": ")
my_function(1, 2, 3, name='Alice', age=30)
# 输出:
# 1
# 2
# 3
# name: Alice
# age: 30