函数的功能

名称空间的作用域

1.作用

名称空间所能够作用的范围

2.内置名称空间

程序任何阶段任何位置均可使用(全局有效)

3.全局名称空间

程序任何阶段任意位置均可使用(全局有效)

4.局部名称空间

一般情况下只在各自的局部名称空间中有效

global与nonlocal关键字使用

1 # x = 111
2 # def index():
3 #     # 局部修改全局变量 需要使用关键字声明
4 #     global x
5 #     x = 222
6 # index()
7 # print(x)

 

 

1 # name_list = ['jason', 'kevin']
2 # def index():
3 #     name_list.append('heiheihei')
4 # index()
5 # print(name_list)

如果想在局部修改全局数据

   如果数据为不可变类型则需要关键字global声明

   如果数据为可变类型则无需关键字globai声明

1 def index():
2     # x = 111
3     l1 = [11,22]
4     def func():
5         # 内部局部修改外部局部
6         l1.append(333)
7     func()
8     print(l1)
9 index()

如果想要在内部的局部修改外部局部的不可变类型数据
需要关键字nonlocal声明

函数对象

又名函数名

函数名遇到括号就会调用(重点)

用法1:函数名可以当做变量名赋值

1 def index():
2     print('from index')
3 
4 
5 a = index  # 赋值
6 a()  # 调用

就是在调用lndex的函数

用法2:函数名还可以当做函数的实参

 1 def index():
 2     print('from index')
 3 
 4 
 5 def func(a):
 6     print(a)
 7     a()
 8     print('from func')
 9 
10 
11 func(index)

用法3:函数名还可以当做函数返回值

 1 def index():
 2     print('from index')
 3 
 4 
 5 def func():
 6     print('from func')
 7     return index
 8 
 9 
10 res = func()   # 调用func并接受func的返回值
11 res()

用法4:函数名可以当做容器类型(内部可以存放多个数据)的元素

def index():
    print('from index')


l = [111, 222, 333, index()]
print(l)
 1 def register():
 2     print('注册功能')
 3 
 4 
 5 def login():
 6     print('登陆功能')
 7 
 8 
 9 def recharge():
10     print('充值功能')
11 
12 
13 def see():
14     print('查看余额')
15 
16 
17 def shopping():
18     print('查看购物车')
19 
20 
21 def empty():
22     print('清空购物车')
23 
24 
25 func_dic = {'1': register,
26             '2': login,
27             '3': recharge,
28             '4': see,
29             '5': shopping,
30             '6': empty
31             }
32 while True:
33     print("""
34     1.注册功能
35     2.登陆功能
36     3.充值功能
37     4.查看余额
38     5.查看购物车
39     6.清空购物车
40     """)
41     choice = input('请输入功能编号》》》:').strip()
42     if choice in func_dic:
43         func_name = func_dic.get(choice)
44         func_name()
45     else:
46         print('功能不存在')

 

 

函数的嵌套调用

函数体内部定义其他函数

1 # def index():
2 #     print('from index')
3 # def func():
4 #     index()
5 #     print('from func')
6 # func()
 1 def my_max(a, b):
 2     if a > b:
 3         return a
 4     return b
 5 
 6 def many_max(x,y,z,m):
 7     res = my_max(x,y)
 8     res1 = my_max(res,z)
 9     res2 = my_max(res1,m)
10     return res2
11 ret = many_max(1,2,3,4)
12 print(ret)7

函数的嵌套定义

函数体内部定义其他函数

 1 def all_func(type):
 2     def register():
 3         print('注册功能')
 4     def login():
 5         print('登录功能')
 6     def transfer():
 7         print('转账功能')
 8     def shopping():
 9         print('购物功能')
10     # 这里仅仅是延时嵌套定义的现象 暂不考虑优化
11     if type == '1':
12         register()
13     elif type == '2':
14         login()
15     elif type == '3':
16         transfer()
17     elif type == '4':
18         shopping()
19     else:
20         print('不知道啥功能')
21 
22 all_func('3')

 

 

闭包函数

闭:定义在函数内部的函数

包:内部函数使用了外部函数名称空间中的名字

只有符合上述两个特征的函数才可以称之为"闭包函数"

1 def outer():
2     x = 222
3 
4     def index():
5         print('from index', x)
6     return index

闭包函数其实是给函数传参的第二种方式

方式1:函数体代码需要用到数据 直接在括号内定义形参即可

1 def index(username):
2     print(username)
3 
4 
5 def my_max(a, b):
6     if a > b:
7         return a
8     return b

方式2:利用闭包函数

 1 def outer(x, y):
 2     # x = 2
 3     # y = 40
 4     def my_max():
 5         if x > y:
 6             return x
 7         return y
 8 
 9     return my_max
10 
11 
12 res = outer(2, 40)
13 print(res())
14 print(res())
 1 import requests
 2 url = 'https://www.baidu.com'
 3 def outer(url):
 4     def get_content():
 5         # 爬取百度首页数据
 6         res = requests.get(url)
 7         if res.status_code == 200:
 8             with open(r'xxx.html','wb') as f:
 9                 print('*'.center(30))
10                 f.write(res.content)
11     return get_content
12 
13 res = outer('https://www.baidu.com')
14 res()
15 res()
16 res()
17 res1 = outer('https://www.jd.com')
18 res1()
19 res1()
20 res1()
21 res1()

 

 

装饰器

装饰器并不是一个新的知识

而是由我们之前所学习的 名称空间 函数对象 闭包函数组合而来

器:指的是工具
装饰:给被装饰对象添加额外的功能

装饰器的原则
开放封闭原则
开发:对扩展开放
封闭:对修改封闭

装饰器核心思想

在不改变被"装饰对象内部代码"和"原有调用方式"的基础之上添加额外功能

 1 def index():
 2     print('from index')
 3 index()
 4 
 5 # 统计index函数的执行时间
 6 
 7 
 8 import time
 9 
10 # print(time.time())  # 1637036293.0609405
11 # 获取的结果叫时间戳(运行代码的那一刻距离1970-1-1所经历的秒数)
12 # time.sleep(3)  # 让程序原地等待3秒
13 # print('睡饱了')
14 
15 
16 def index():
17     time.sleep(3)
18     print('from index')
19 # 统计index函数的执行时间
20 # 在函数运行之前统计一次时间
21 start_time = time.time()
22 index()
23 # 在函数运行完毕之后再次统计
24 end_time = time.time()
25 # 计算差值
26 print(end_time - start_time)

 

 

posted @ 2022-01-07 19:00  colagirl  阅读(228)  评论(0)    收藏  举报