函数
- 函数
- 函数定义与调用
- 形参和实参
- 变量的作用域(全局变量和局部变量)
- 局部变量和全局变量的测试
- 参数的传递
- 参数的几种类型
- 位置参数
- 默认值参数
- 命名参数
- 可变参数
- 强制命名参数
- lambda表达式和匿名函数
- eval()函数用法
- 递归函数_函数调用内存分析_栈帧的创建
- 嵌套函数_内部函数_数据隐藏
- nonlocal和global 声明变量
- LEGB规则
函数


函数定义与调用


形参和实参



变量的作用域(全局变量和局部变量)


局部变量和全局变量的测试
import math
import timedef ceshi01():start=time.time()for i in range(1000000):math.sqrt(30)end =time.time()print('耗时{0}'.format((end-start)))def ceshi02():b= math.sqrtstart=time.time()for i in range(1000000):b(30)end = time.time()print('耗时{0}'.format((end - start)))
ceshi01()
ceshi02()
耗时0.16200947761535645
耗时0.1390080451965332Process finished with exit code 0
参数的传递




import copydef testCopy():''' 测试浅拷贝'''a=[10,20,[5,6]]b=copy.copy(a)print('a:',a)print('b:',b)b.append(30)b[2].append(7)print('浅拷贝...')print('a:', a)print('b:', b)
testCopy()def testDeepCopy():''' 测试深拷贝'''a=[10,20,[5,6]]b=copy.deepcopy(a)print('a:',a)print('b:',b)b.append(30)b[2].append(7)print('深拷贝...')print('a:', a)print('b:', b)
testDeepCopy()
a: [10, 20, [5, 6]]
b: [10, 20, [5, 6]]
浅拷贝...
a: [10, 20, [5, 6, 7]]
b: [10, 20, [5, 6, 7], 30]
a: [10, 20, [5, 6]]
b: [10, 20, [5, 6]]
深拷贝...
a: [10, 20, [5, 6]]
b: [10, 20, [5, 6, 7], 30]Process finished with exit code 0


参数的几种类型
位置参数

默认值参数

命名参数


可变参数


强制命名参数

lambda表达式和匿名函数



eval()函数用法



递归函数_函数调用内存分析_栈帧的创建





嵌套函数_内部函数_数据隐藏



nonlocal和global 声明变量



LEGB规则



