函数Function
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。
定义一个函数
你可以定义一个由自己想要功能的函数,以下是简单的规则:
- 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 ()。
- 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数。
- 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。
- 函数内容以冒号起始,并且缩进。
- return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def function_name(parameter): '''Annotations''' Logic *** return Value
返回值 return
#return Value; # *The first return ends. # return value = 0, return None # return value = 1, return Object # return value > 1, return Tuple
参数parameter
默认参数 *default parameters
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# z is default parameter. def count(x, y, z=3): "annotations" v = x + y + z return v print(count(1, 2))
位置参数 *position parameters
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# all is position parameter. def count(x, y, z): "annotations" v = x + y + z return v print(count(1, 2, 3))
关键字参数 *keyword parameters
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# y,z is keywords parameter def count(x, y, z): 'annotations' v = x + y + z return v print(count(1, y=2, z=3))
接收参数 *args\**keyargs
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # *args Received all undefined position parameter as a Tuple. 2 def count(x, *args): 3 print(x) 4 print(args) 5 6 7 count(1, 2, 3, 4, 5)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# **kwargs Received all undefined keywords parameter as a Dict. def count(x, **kwargs): print(x) print(kwargs) count(1, y=6, z=7)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# Universal, Receive all. def count(x, *args, **kwargs): print(x) print(args) print(kwargs) count(1, 2, 3, 4, 5, y=6, z=7)
参数引用parameters cite
引用列表 *list
引用字典 **dict
全局变量global
- 如果函数的内容无global关键字,优先读取局部变量,能读取全局变量,无法对全局变量重新赋值,但是对于可变类型,可以对内元素进行操作。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
variable_NAME = ['variable_value1', 'variable_value2'] def function_name(): NAME = 'variable_value' print(NAME) function_name()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
variable_NAME = ['variable_value1', 'variable_value2'] def function_name(): NAME.append('variable_value3') print(NAME) function_name()
- 如果函数中有global关键字,变量本质就是全局变量,可读可赋值;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
variable_NAME = ['variable_value1', 'variable_value2'] def function_name(): global NAME variable_NAME = 'variable_value' print(NAME) function_name()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
variable_NAME = ['variable_value1', 'variable_value2'] def function_name(): global NAME variable_NAME = ['variable_value'] variable_NAME.append('variable_value3') print(NAME) function_name()
- 规范
全局变量为大写;
局部变量为小写
如不规范:同一函数体内,全局变量global关键字必须在局部变量前面,否则报错;
- nonlocal:指定上级变量
前向理论
递归
- 必须有明确的结束条件
- 每次进入更深开天一层递归时,问题规模相比上次递归都应有减少
- 递归效率不高,递归层次过多会导致栈溢出
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time person_list = ['fj', 'gy', 'zl', 'yf', 'ym', 'cy', 'zq'] def ask_way(person_list): print('-' * 60) if len(person_list) == 0: return "YES" y = person_list.pop(0) # print(y) if y == 'zq': return "NO" print("%s , I not know" %y) time.sleep(1) res = ask_way(person_list) return res print(ask_way(person_list))
作用域
作用域在定义函数时就已经固定了,不会随着调用位置的改变而改变.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = 'abc' def test(): name = 'efg' def test1(): name = 'hij' def test2(): print(name) return test2 return test1 test()()()
匿名函数
lambda x:x**2
- x :形参;
- x**2 :逻辑式;
- 逻辑式默认return;
- 一般情况下会和其它函数合并使用,并不是单独使用;
- 无复杂逻辑(符合return) 如:if\for\while等;
- 多形参,多逻辑式时匿名函数需自加元组符号,return默认有元组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = 's1' test = lambda x: x + 's2' print(test(name))
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
test1 = lambda x, y, z: (x + 1, y + 1, z + 1) print(test1(1, 2, 3))
高阶函数
调用函数时并行了其它函数.
符合其中一条即为高阶函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def fun1(s1): print(s1) def fun2(x): print('%s is test print' % x) fun1(fun2('test'))
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def fun1(): print('This is fun1') return fun2 def fun2(): print('This is fun2') fun1()()