目录:
1、装饰器
2、迭代器&生成器
3、Json & pickle 数据序列化
4、软件目录结构规范
一、装饰器
定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
原则:
1、不能修改被装饰的函数的源代码
2、不能修改被装饰的函数的调用方式
一个简单的装饰器:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Author :GU 4 import time 5 def timmer(func): ##后加的一个记录运行时间的装饰器 6 def warpper(*args,**kwargs): 7 start_time= time.time() 8 func() 9 stop_time=time.time() 10 print("The func run timr is %s"%(stop_time-start_time)) 11 return warpper 12 @timmer ##把装饰器调用到函数test1函数里面去 13 def test1(): 14 time.sleep(3) 15 print("In the test1") 16 test1() 17 执行结果: 18 In the test1 19 The func run timr is 3.000171661376953
实现装饰器知识储备:
1、函数即变量
2、高阶函数
3、嵌套函数
高阶函数+嵌套函数=装饰器
知识点一:函数即变量==》函数的调用顺序
其他高级语言类似,Python 不允许在函数未声明之前,对其进行引用或者调用
①、错误的示范:
2 def foo(): 3 print ('in the foo') 4 bar() 5 foo() 6 报错: 7 in the foo 8 Traceback (most recent call last): 9 File "<pyshell#13>", line 1, in <module> 10 foo() 11 File "<pyshell#12>", line 3, in foo 12 bar() 13 NameError: global name 'bar' is not defined 14 ===================================== 15 def foo(): 16 print ('foo') 17 bar() 18 foo() 19 def bar(): 20 print ('bar') 21 报错:NameError: global name 'bar' is not define
②、正确的示范
1 def bar(): 2 print ('in the bar') 3 def foo(): 4 print ('in the foo') 5 bar() 6 7 foo() 8 =================================== 9 def foo(): 10 print ('in the foo') 11 bar() 12 def bar(): 13 print ('in the bar') 14 foo() 15 执行结果: 16 in the foo 17 in the bar 18 ======================= 19 in the foo 20 in
知识点二:
高阶函数:a、把一个函数名当作实参传给另外一个函数 b、返回值中包含函数名
按a原则定义一个函数:
1 #不修改被装饰函数源代码的情况下为其添加功能 2 import time 3 def bar(): 4 time.sleep(3) 5 print('in the bar') 6 7 def test1(func): 8 start_time=time.time() 9 func() #run bar 10 stop_time=time.time() 11 print("the func run time is %s" %(stop_time-start_time)) 12 13 test1(bar) 14 执行结果: 15 in the bar 16 the func run time is 3.000171661376953
按b原则定义一个函数:
1 #不修改函数的调用方式 2 import time 3 def bar(): 4 time.sleep(3) 5 print('in the bar') 6 def test2(func): 7 print(func) 8 return func 9 10 # print(test2(bar)) 11 bar=test2(bar) 12 bar() #run bar 13 执行结果: 14 <function bar at 0x00000000007DE048> 15 in the bar
知识点三:
嵌套函数:定义:在一个函数体内创建另外一个函数,这种函数就叫内嵌函数(基于python支持静态嵌套域)
1 def foo(): 2 print('in the foo') 3 def bar(): 4 print('in the bar') 5 bar() 6 foo() 7 执行结果: 8 in the foo 9 in the bar
局部作用域和全局作用域的访问顺序
1 x=0 2 def grandpa(): 3 x=1 4 def dad(): 5 x=2 6 def son(): 7 x=3 8 print(x) 9 son() 10 dad() 11 grandpa() 12 执行结果: 13 3
一个完整的装饰器:
不带参数的:
1 import time 2 def timer(func): #timer(test1) func=test1 3 def deco(): 4 start_time=time.time() 5 func() #run test1() 6 stop_time = time.time() 7 print("the func run time is %s" %(stop_time-start_time)) 8 return deco 9 @timer #test1=timer(test1) 10 def test1(): 11 time.sleep(1) 12 print('in the test1') 13 #执行结果: 14 in the test1 15 the func run time is 1.0000572204589844
带参数的:
1 import time 2 def timer(func): #timer(test1) func=test1 3 def deco(*args,**kwargs): 4 start_time=time.time() 5 func(*args,**kwargs) #run test1() 6 stop_time = time.time() 7 print("the func run time is %s" %(stop_time-start_time)) 8 return deco 9 @timer #test1=timer(test1) 10 def test1(): 11 time.sleep(1) 12 print('in the test1') 13 @timer # test2 = timer(test2) = deco test2(name) =deco(name) 14 def test2(name,age): 15 time.sleep(1) 16 print("test2:",name,age) 17 test1() 18 test2("alex",22) 19 执行结果: 20 in the test1 21 the func run time is 1.0000572204589844 22 test2: alex 22 23 the func run time is 1.000057220458984
终极版装饰器:
user,passwd = 'alex','abc123' def auth(auth_type): print("auth func:",auth_type) def outer_wrapper(func): def wrapper(*args, **kwargs): print("wrapper func args:", *args, **kwargs) if auth_type == "local": username = input("Username:").strip() password = input("Password:").strip() if user == username and passwd == password: print("