上节回顾:

1.集合 a.关系测试 b.去重 2.文件操作及编码 3.函数 4.局部变量和全局变量
本节课内容:
1.迭代器生成器
2.装饰器
3.json pickle数据序列化
4.软件目录结构规范
5.作业:ATM项目开发
一、装饰器
1. 定义:本质是函数,用来装饰其他函数,就是为其他函数添加附加功能
2. 原则:
A. 不能修改被装饰的函数的源代码
B. 不能修改被装饰的函数的调用方式
3.实现装饰器知识储备
A. 函数即“变量”
B. 高阶函数
a. 把一个函数名当做实参传给另一个函数
b. 返回值中包含函数名(不修改函数的调用方式)
C. 嵌套函数
高阶函数+嵌套函数=>装饰器
牛刀初试:
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" import time def timmer(func): def warpper(*args,**kwargs): start_time=time.time() func() stop_time=time.time() print("in the func time is %s" %(stop_time-start_time)) return warpper @timmer def test1(): time.sleep(3) print("in the test1") test1()
内容详解:
一:函数调用顺序:其他高级语言类似,Python 不允许在函数未声明之前,对其进行引用或者调用
错误示范:
def foo(): print 'in the foo' bar() foo() 报错: in the foo Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> foo() File "<pyshell#12>", line 3, in foo bar() NameError: global name 'bar' is not defined
错误:bar没有定义
def foo(): print 'foo' bar() foo() def bar(): print 'bar'
报错:
NameError: name 'bar' is not defined
错误:执行foo时还没执行到bar定义
正确示范:(注意,python为解释执行,函数foo在调用前已经声明了bar和foo,所以bar和foo无顺序之分)
def bar(): print("in the bar") def foo(): print("in the foo") bar() foo() print("==========") def foo(): print("in the foo") bar() def bar(): print("in the bar") foo()
输出结果:
in the foo in the bar
==========
in the foo
in the bar
4.匿名函数
python 使用 lambda 来创建匿名函数。
所谓匿名,意即不再使用 def 语句这样标准的形式定义一个函数。
- lambda 只是一个表达式,函数体比 def 简单很多。
- lambda的主体是一个表达式,而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。
- lambda 函数拥有自己的命名空间,且不能访问自有参数列表之外或全局命名空间里的参数。
- 虽然lambda函数看起来只能写一行,却不等同于C或C++的内联函数,后者的目的是调用小函数时不占用栈内存从而增加运行效率。
语法
lambda 函数的语法只包含一个语句,如下:
lambda [arg1 [,arg2,.....argn]]:expression
如下实例:
#!/usr/bin/python3 # 可写函数说明 sum = lambda arg1, arg2: arg1 + arg2; # 调用sum函数 print ("相加后的值为 : ", sum( 10, 20 )) print ("相加后的值为 : ", sum( 20, 20 ))
以上实例输出结果:
相加后的值为 : 30
相加后的值为 : 40
例:
calc= lambda x:x*3 print(calc(3))
输出结果
9
5.高阶函数
把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式。
牛刀初试1
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" def bar(): print("in the bar") def test(function): print(function) test(bar)
输出结果:
<function bar at 0x00000000023D3488>
***上面输出为内存地址
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" def bar(): print("in the bar") def test(function): print(function) function() #调用 test(bar)
输出结果:
<function bar at 0x00000000023E3488>
in the bar
例:
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" import time def bar(): time.sleep(3) print("in the bar") def test(function): start_time=time.time() function() stop_time=time.time() print("the functon run time is %s" %(stop_time-start_time)) test(bar)
输出结果:
in the bar the functon run time is 3.000171661376953
牛刀初试2
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" import time def bar(): time.sleep(3) print("in the bar") def test2(function): print(function) return function # print(test2(bar)) # t=test2(bar) # t() # bar=test2(bar) # bar() run bar
6.嵌套函数
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" def foo(): print("In the foo") def bar(): print("In the bar") bar() foo() 注意区分下面情况:函数的调用 def test1(): test2() test1()
7.装饰器;
a. 小高潮
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" import time def timmer(function): #timer(test1) function=test1 def deco(): start_time=time.time() function() stop_time=time.time() print("in the func time is %s" %(stop_time-start_time)) return deco @timmer # test1=timmer(test1) def test1(): time.sleep(3) print("in the test1") def test2(): time.sleep(3) print("in the test2") test1() test2()
输出结果:
in the test1 in the func time is 3.000171661376953 in the test2
b.中高潮
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" import time def timmer(function): #timer(test1) function=test1 def deco(*args,**kwargs): start_time=time.time() function(*args,**kwargs) stop_time=time.time() print("in the func time is %s" %(stop_time-start_time)) return deco @timmer # test1=timmer(test1) == deco test2(name) = deco(name) def test1(): time.sleep(1) print("in the test1") @timmer # test1=timmer(test2) def test2(name,age): time.sleep(5) print("test2 is ",name,age) test1() test2("alex",32)
c. 终极版
#!/usr/bin/python # -*- conding:utf-8 -*- __Author__ = "YoungCheung" import time user,passwd='alex','abc123' def auth(auth_type): print("auth function",auth_type) def outer_wrapper(function): def wrapper(*args,**kwargs): print("auth function args",*args,**kwargs) if auth_type == "local": username=input("UserName:").strip() password=input("PassWord:").strip() if user== username and passwd==password: print("