闭包 装饰器 - 总结

1.闭包:
函数内部的子函数返回,在外部调用子函数时,其实函数已经结束,但是在调用子函数时,函数内部的局部变量任然有效。

 1 def fun():
 2     n = 10
 3     def fun1():
 4         print('n为:', n)
 5     return fun1
 6 
 7 f = fun()
 8 print(f)
 9 f()
10 
11 <function fun.<locals>.fun1 at 0x000001DF0E6DFA60>
12 n为: 10

--------------------------------------------------

2.装饰器:
参考:http://www.cnblogs.com/alex3714/articles/5765046.html

软件开发的一个原则:开放-封闭 原则
    开放:对现有功能的扩展开放
    封闭:已实现的功能代码块不应该被修改

------------------

# 对河南和欧美专区认证,若是已经登录过,就不需要在登录。

# 2.1.语法糖 装饰器 

 1 user_state = False
 2 def login(func):
 3     def inner():
 4         _username = 'alice'
 5         _password = '123'
 6         global user_state
 7         if user_state == False:
 8             username = input('username:').strip()
 9             password = input('password:').strip()
10 
11             if username == _username and password == _password:
12                 print('welcome to login...')
13                 user_state = True
14             else:
15                 print('wrong username or password')
16         else:
17             print('用户已经登录,认证通过')
18         if user_state == True:
19             func()
20     return inner
21 
22 def home():
23     print('-----首页-----')
24 @login # america=login(america)
25 def america():
26     print('-----欧美专区-----')
27 
28 def japan():
29     print('-----日韩专区-----')
30 @login # henan=login(henan)
31 def henan():
32     print('-----河南专区-----')
33 
34 home()
35 henan()
36 america() 
37 
38 -----首页-----
39 username:alice
40 password:123
41 welcome to login...
42 -----河南专区-----
43 用户已经登录,认证通过
44 -----欧美专区-----
语法糖 装饰器

# 2.2.带参数的装饰器

 1 user_state = False
 2 def login(func):
 3     def inner(*args, **kwargs):
 4         _username = 'alice'
 5         _password = '123'
 6         global user_state
 7         if user_state == False:
 8             username = input('username:').strip()
 9             password = input('password:').strip()
10 
11             if username == _username and password == _password:
12                 print('welcome to login...')
13                 user_state = True
14             else:
15                 print('wrong username or password')
16         else:
17             print('用户已经登录,认证通过')
18         if user_state == True:
19             func(*args, **kwargs) 
20     return inner
21 
22 def home():
23     print('-----首页-----')
24 @login
25 def america():
26     print('-----欧美专区-----')
27 @login
28 def japan(x, y, z=5):
29     print('-----日韩专区-----', x, y, z) 
30 @login
31 def henan(x):
32     print('-----河南专区-----', x) 
33 
34 home()
35 henan('haha')
36 america()
37 japan('japan', 1, z=7)
38 
39 -----首页-----
40 username:alice
41 password:123
42 welcome to login...
43 -----河南专区----- haha
44 用户已经登录,认证通过
45 -----欧美专区-----
46 用户已经登录,认证通过
47 -----日韩专区----- japan 1 7
带参数的装饰器

# 2.3.带参数2的装饰器

 1 user_state = False
 2 def login(auth_type):
 3     def outer(func):
 4         def inner(*args, **kwargs):
 5             _username = 'alice'
 6             _password = '123'
 7             global user_state
 8             if user_state == False:
 9                 username = input('username:').strip()
10                 password = input('password:').strip()
11 
12                 if username == _username and password == _password:
13                     print('welcome to login...')
14                     user_state = True
15                 else:
16                     print('wrong username or password')
17             else:
18                 print('用户已经登录,认证通过')
19             if user_state == True:
20                 func(*args, **kwargs)
21         return inner
22     return outer
23 
24 def home():
25     print('-----首页-----')
26 @login('wx')
27 def america():
28     print('-----欧美专区-----')
29 @login('wb')
30 def japan(x, y, z=5):
31     print('-----日韩专区-----', x, y, z)
32 @login('qq')  #henan = login('qq')(henan) =inner
33 def henan(x):
34     print('-----河南专区-----', x)
35 
36 home()
37 # xx = login('qq')
38 # henan = xx(henan)
39 henan('haha')
40 america()
41 japan('japan', 1, z=7)
42 
43 -----首页-----
44 username:alice
45 password:123
46 welcome to login...
47 -----河南专区----- haha
48 用户已经登录,认证通过
49 -----欧美专区-----
50 用户已经登录,认证通过
51 -----日韩专区----- japan 1 7
带参数2的装饰器
posted @ 2018-02-17 22:08  Alice的小屋  阅读(226)  评论(0编辑  收藏  举报