Function of the structure:
Function's return value:
1. When the function execution fortune to return keywords will no longer execute down
def func(): print("hello world!") print("hello world!") return print("hello world!") func() #hello world! #hello world!
2. When the function does not return to write or not write a value back return it returns None
def func(): print("hello world!") print(func()) # hello world! # None
3. When a write back function return value returns that value
4. When a write back function of the result of a plurality of tuples is returned, the plurality of variables may be obtained by directly using ancestral
def func(): return 1,5,6,7 values = func() print(values) #(1, 5, 6, 7)
Why is a return to the ancestral data?
When opening the python interpreter
python automatically combined into a comma-delimited elements tuples, tuples, lists, dictionaries can unpack
Function parameters:
Parameter passing: that is passed to the actual parameters of the process parameters in the form of
<1> formal parameters: parameter variables used in the function body
. A position parameter: from left to right according to the position of one-
The default value of the parameter b: setting a default value for the function parameter, such as when we call our open function mode is the default mode = 'r', we do not write omitted
def print_info(name,age,sex = 'male'): print("{} {} {}".format(name, age,sex)) print_info('Jane',age = 15) #Jane male 15
The default value may also be to modify the actual parameters:
def print_info(name,age,sex = 'male'): print("{} {} {}".format(name, age,sex)) print_info('Jane',sex = 'female',age = 15) #Jane female 15
陷阱:如果默认参数的值是一个可变数据类型,那么每一次调用函数的时候,如果不传值那么就公用这个数据类型的资源
默认参数为列表:
def func(li = []): li.append(2) print(li) func() #[2] func() #[2, 2] func([]) #[2] func() #[2, 2, 2]
默认参数为字典:
def func(dic = {}): dic['a'] = 'v' print(dic) func() #{'a': 'v'} key是唯一所以每一次都是覆盖 func() #{'a': 'v'} func({}) #{'a': 'v'} func() #{'a': 'v'}
def func(k,dic = {}): dic[k] = 'v' print(dic) func('a') #{'a': 'v'} func('b') #{'a': 'v', 'b': 'v' func('c') #{'a': 'v', 'b': 'v', 'c': 'v'}
c.动态参数:
①动态接收位置参数:*args 接收所有的位置参数,再以元祖的形式赋予args
def sum_data(*args): res = 0 for i in args: res += i return res print(sum_data(1,2,3,4,7,8,-10)) #15
②动态接收关键字参数:**kwargs 接收所有的关键字参数,再以字典的形式赋予kwargs
def print_data(**kwargs): print(kwargs) print_data(name='Jane',sex='female',age=22) #{'name': 'Jane', 'sex': 'female', 'age': 22}
两种不同类型的动态参数可以一起使用,这样会大大提升参数扩展性,也解决了大多数实参一一对应的难题
<2>实际参数:在调用函数时使用的参数变量
a.位置参数:从左到右和形参的位置一一对应
def print_info(name,sex,age): print("{} {} {}".format(name,sex,age)) print_info('Jane','female',15) #Jane female 15
b.关键字参数:当用于繁杂的数据时,不需要记住参数的位置,只需要记住参数的名字即可
def print_info(name,sex,age): print("{} {} {}".format(name, sex, age)) print_info(name='Jane',sex='female',age = 15) #Jane female 15
c.混合参数:就是位置参数和关键字参数可以混合着使用
原则:当使用这种写法就一定要注意位置参数必须要在关键字参数之前
def print_info(name,sex,age): print("{} {} {}".format(name, sex, age)) print_info('Jane','female',age = 15) #Jane female 15
*的用法: