Files
Python_CookBook_repo/7.函数/1.编写可接受任意数量参数的函数.py
2025-09-10 16:12:45 +08:00

16 lines
478 B
Python

# 要编写一个接受任意数量参数的函数,可以使用*
def avg(first, *rest):
return (first + sum(rest))/(len(rest) + 1)
# 如果是关键字类型的参数,那就需要使用**
def make_element(name, value, **attrs):
for item in attrs.items():
print("'{} = {}'".format(*item))
return 0
# *与**有使用上的限制,它们必须在所有参数的最后,**必须在*后面
def anyargs(*args, **kwargs):
print(args)
print(kwargs)