5200.8.6 默认参数

Default Values

(When writing a function…) 当你写函数的时候,可以给每一个参数一个默认的值。如果函数调用的时候,提供了值,就只用提供的值。否者用默认值。

(For example, if you notice) 你可以给一个函数的参数定义一个常用的默认值

def describe_pet(pet_name, animal_type='dog'):
    """Display information about a pet."""
    print(f"\nI have a {animal_type}.")
    print(f"My {animal_type}'s name is {pet_name.title()}.")
 
describe_pet(pet_name='willie')

(Note that the order of…) 有默认值的参数应该放在后面。