4  函数

4.1 参数

4.1.1 前两个参数是固定的,而后面的参数是从字典解包得到

def my_function(arg1, arg2, **kwargs):
    # 使用arg1和arg2
    print("arg1:", arg1)
    print("arg2:", arg2)
    
    # 使用字典解包后的参数
    for key, value in kwargs.items():
        print(f"{key}: {value}")

# 调用函数
my_function("fixed1", "fixed2", key1="value1", key2="value2")
arg1: fixed1
arg2: fixed2
key1: value1
key2: value2
def print_info(name, age, **details):
    print("Name:", name)
    print("Age:", age)
    
    for key, value in details.items():
        print(f"{key}: {value}")

# 调用函数
print_info("Alice", 30, city="New York", occupation="Engineer")
Name: Alice
Age: 30
city: New York
occupation: Engineer