Python function small note

Excerpt from "Python Programming: from entry to practice" Eric Matthes forward to page 70

When writing function, you can specify a default value for each parameter type. When the call to the function parameter arguments provided, Python be specified argument values; otherwise, the default value of parameter. Therefore, the parameter to specify a default value, may be omitted respective arguments in the function call. Use the default value simplify the function calls, but also clearly pointed out that typical use of the function.

For example, if you find call describe_pet (), are described in a large dog, the default parameter values ​​can be set to animal_type 'dog'. Thus, call describe_pet () will be described when the dog, can not provide such information:

def describe_pet(pet_name, animal_type='dog'):
 """显示宠物的信息"""
 print("\nI have a " + animal_type + ".")
 print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(pet_name='willie')

To copy the list passed to the function, you can do something like this:

function_name(list_name[:])

 

  If a function of the input parameter is defined with an asterisk, it will create a tuple (tuple) in the function, and enclosed tuple all received values

Guess you like

Origin www.cnblogs.com/yyf2019/p/11582619.html