python学习第四天---2019年5月14日

本文深入解析Python装饰器的定义、实现原理及应用场景,包括如何为函数添加额外功能而不修改其源代码,以及如何利用生成器实现高效的数据处理。通过实际代码示例,详细展示了装饰器的构造过程和生成器的工作机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第四周-第03章节-Python3.5-装饰器详解

装饰器:

定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能

原则:1、不能修改被装饰的函数的源代码

           2、不能修改被装饰的函数的调用方式

 

实现装饰器知识储备:

1、函数即是“变量”

2、高阶函数

     a: 把一个函数名当做实参传给另外一个函数(在不修改被装饰函数源代码的情况下为其添加功能)

     b.返回值中包含函数名(不能修改被装饰的函数的调用方式)

3、嵌套函数

高阶函数+嵌套函数 ======》装饰器

 

第四周-第04章节-Python3.5-装饰器应用详解、

decorator.py
#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: decorator.py
@time: 2019/05/14
@functions:装饰器
@notice:
"""
import time
def timmer(func):
def warpper(*args,**kwargs):
start_time=time.time()
func()
stop_time=time.time()
print("the func run time is %s" %(stop_time-start_time))
return warpper

@timmer
def test1():
time.sleep(3)
print("in the test1")

test1()
=================================================================================================

D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/day4/decorator.py
in the test1
the func run time is 3.0001447200775146

Process finished with exit code 0

 

第四周-第05章节-Python3.5-装饰器之函数即变量

decorator1.#-*- coding:utf-8 _*""" 
@author:Lenovo 
@file: decorator1.py
@time: 2019/05/14
@functions:装饰器
@notice:
"""
'''
def foo():
print("in the foo")
bar()
foo()
报错,bar()没有被定义
'''
'''
print("=======================================")
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()
'''
print("===========================================")
def foo():
print("in the foo")
bar()
foo()
def bar():
print("in the bar")
报错:函数没有被定义


第四周-第06章节-Python3.5-装饰器之高阶函数
#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: decorator2.py
@time: 2019/05/14
@functions:装饰器
@notice:统计Bar函数运行的时间
"""
import time
'''
def bar():
time.sleep(3)
print("in the bar")

def test1(func):
start_time=time.time()
# print(func)
func() #return bar
stop_time=time.time()
print("the func run time is %s" %(stop_time-start_time))

test1(bar)
'''

def bar():
time.sleep(3)
print("in the bar")

def test2(func):
print(func)
return func

print(test2(bar)) #仅仅返回内存地址
bar=test2(bar)
bar() #返回函数结果

=================================================================================================

ssh://root@192.168.1.106:22/usr/bin/python -u /www/web/老男孩S14/day4/decorator2.py
<function bar at 0x7fde1db911e0>
<function bar at 0x7fde1db911e0>
<function bar at 0x7fde1db911e0>
in the bar

Process finished with exit code 0

 

第四周-第07章节-Python3.5-装饰器之嵌套函数

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: decorator3.py
@time: 2019/05/18
@functions:装饰器之嵌套函数
@notice:
"""
def foo():
print("in the foo")
def bar():
print("in the bar")
bar()

foo()

#作用域与全局作用域的访问顺序
x = 0
def grandpa():
# x = 1
def dad():
x = 2
def son():
x = 3
print (x)
son()
dad()
grandpa()
===============================================================================================

ssh://root@192.168.1.106:22/usr/bin/python -u /www/web/老男孩S14/day4/decorator3.py
in the foo
in the bar
3

Process finished with exit code 0

 

 

第四周-第08章节-Python3.5-装饰器之案例剖析1

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: decorator4.py
@time: 2019/05/18
@functions:装饰器
@notice:再不改变函数调用方式的情况下,统计test1函数运行的时间
调用装饰器,是在函数的前面加上@符号并且连接函数名即可
"""
import time
def timer(func):
def deco():
start_time=time.time()
func()
stop_time=time.time()
print("the func run time is %s"%(stop_time-start_time))
return deco

@timer
def test1():
time.sleep(3)
print("in the test1")

@timer
def test2():
time.sleep(3)
print("in the test2")

#test1=timer(test1)
test1()
test2()

========================================================================================

ssh://root@192.168.1.106:22/usr/bin/python -u /www/web/老男孩S14/day4/decorator4.py
in the test1
the func run time is 3.003230094909668
in the test2
the func run time is 3.003512382507324

Process finished with exit code 0

 

第四周-第09章节-Python3.5-装饰器之案例剖析2

通用函数,给test2传值

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: decorator4.py
@time: 2019/05/18
@functions:装饰器
@notice:再不改变函数调用方式的情况下,统计test1函数运行的时间
调用装饰器,是在函数的前面加上@符号并且连接函数名即可
"""
import time
def timer(func):
def deco(*args,**kwargs):
start_time=time.time()
func(*args,**kwargs)
stop_time=time.time()
print("the func run time is %s"%(stop_time-start_time))
return deco

@timer
def test1():
time.sleep(3)
print("in the test1")

@timer
def test2(name,age):
time.sleep(3)
print("test2:",name,age)

#test1=timer(test1)
test1()
test2("alex",22)
====================================================================================================

ssh://root@192.168.1.106:22/usr/bin/python -u /www/web/老男孩S14/day4/decorator4.py
in the test1
the func run time is 3.0034444332122803
test2: alex 22
the func run time is 3.0032384395599365

Process finished with exit code 0

 

第四周-第10章节-Python3.5-装饰器之高潮讲解

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: decorator5.py
@time: 2019/05/18
@functions:
@notice:
"""
import time
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("\033[32;1mUser has passed authentication\033[0m")
res = func(*args, **kwargs) # from home
print("---after authenticaion ")
return res
else:
exit("\033[31;1mInvalid username or password\033[0m")
elif auth_type == "ldap":
print("搞毛线ldap,不会。。。。")

return wrapper
return outer_wrapper

def index():
print("welcome to index page")
@auth(auth_type="local") # home = wrapper()
def home():
print("welcome to home page")
return "from home"

@auth(auth_type="ldap")
def bbs():
print("welcome to bbs page")

index()
print(home()) #wrapper()
bbs()
===============================================================================

ssh://root@192.168.1.106:22/usr/bin/python -u /www/web/老男孩S14/day4/decorator5.py
auth func: local
auth func: ldap
welcome to index page
wrapper func args:
Username:alex
alex
Password:abc123
abc123
User has passed authentication
welcome to home page
---after authenticaion
from home
wrapper func args:
搞毛线ldap,不会。。。。

Process finished with exit code 0

 

第四周-第11章节-Python3.5-迭代器与生成器1

列表生成式:

Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> [i*2 for i in range(10)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>

生成器

通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。

所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器:generator。

要创建一个generator,有很多种方法。第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个generator:

生成器只有在调用时才会生成相应的数据

 

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: generator.py
@time: 2019/05/18
@functions:斐波拉契数列
@notice:
"""
####生成器
L=[i*i for i in range(11)]
print(L)

Z = []
for i in range(11):
Z.append(i * i)
print(Z)

####斐波拉契数列
def fib(max):
n,a,b=0,0,1
while n < max:
print(b)
a,b=b,a+b
n = n + 1
return 'done'
fib(10)

###yield函数
def fib(max):
n,a,b = 0,0,1
while n < max:
yield b
a,b=b,a+b
n = n + 1

f = fib(100)
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
=======================================================================

ssh://root@192.168.1.106:22/usr/bin/python -u /www/web/老男孩S14/day4/生成器/generator.py
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[0]
[0, 1]
[0, 1, 4]
[0, 1, 4, 9]
[0, 1, 4, 9, 16]
[0, 1, 4, 9, 16, 25]
[0, 1, 4, 9, 16, 25, 36]
[0, 1, 4, 9, 16, 25, 36, 49]
[0, 1, 4, 9, 16, 25, 36, 49, 64]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
1
1
2
3
5
8
13
21
34
55
1
1
2
3
5
8

Process finished with exit code 0

 

第四周-第12章节-Python3.5-迭代器与生成器2

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: 生成器并行.py
@time: 2019/05/18
@functions:
@notice:
"""
import time
def consumer(name):
print("%s 准备吃包子啦!" %name)
while True:
baozi = yield

print("包子[%s]来了,被[%s]吃了!" %(baozi,name))

c = consumer("ChenRonghua")
c.__next__()

# b1= "韭菜馅"
# c.send(b1)
# c.__next__()

def producer(name):
c = consumer('A')
c2 = consumer('B')
c.__next__()
c2.__next__()
print("老子开始准备做包子啦!")
for i in range(10):
time.sleep(1)
print("做了1个包子,分两半!")
c.send(i)
c2.send(i)

producer("alex")
===================================================================================

ssh://root@192.168.1.106:22/usr/bin/python -u /www/web/老男孩S14/day4/生成器/生成器并行.py
ChenRonghua 准备吃包子啦!
A 准备吃包子啦!
B 准备吃包子啦!
老子开始准备做包子啦!
做了1个包子,分两半!
包子[0]来了,被[A]吃了!
包子[0]来了,被[B]吃了!
做了1个包子,分两半!
包子[1]来了,被[A]吃了!
包子[1]来了,被[B]吃了!
做了1个包子,分两半!
包子[2]来了,被[A]吃了!
包子[2]来了,被[B]吃了!
做了1个包子,分两半!
包子[3]来了,被[A]吃了!
包子[3]来了,被[B]吃了!
做了1个包子,分两半!
包子[4]来了,被[A]吃了!
包子[4]来了,被[B]吃了!
做了1个包子,分两半!
包子[5]来了,被[A]吃了!
包子[5]来了,被[B]吃了!

Process finished with exit code 0

 

第四周-第13章节-Python3.5-迭代器与生成器并行

迭代器

我们已经知道,可以直接作用于for循环的数据类型有以下几种:

一类是集合数据类型,如listtupledictsetstr等;

一类是generator,包括生成器和带yield的generator function。

这些可以直接作用于for循环的对象统称为可迭代对象:Iterable

可以使用isinstance()判断一个对象是否是Iterable对象:

而生成器不但可以作用于for循环,还可以被next()函数不断调用并返回下一个值,直到最后抛出StopIteration错误表示无法继续返回下一个值了。

*可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator

可以使用isinstance()判断一个对象是否是Iterator对象:

生成器都是Iterator对象,但listdictstr虽然是Iterable,却不是Iterator

listdictstrIterable变成Iterator可以使用iter()函数:

你可能会问,为什么listdictstr等数据类型不是Iterator

这是因为Python的Iterator对象表示的是一个数据流,Iterator对象可以被next()函数调用并不断返回下一个数据,直到没有数据时抛出StopIteration错误。可以把这个数据流看做是一个有序序列,但我们却不能提前知道序列的长度,只能不断通过next()函数实现按需计算下一个数据,所以Iterator的计算是惰性的,只有在需要返回下一个数据时它才会计算。

Iterator甚至可以表示一个无限大的数据流,例如全体自然数。而使用list是永远不可能存储全体自然数的。

 

小结

凡是可作用于for循环的对象都是Iterable类型;

凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;

集合数据类型如listdictstr等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。

Python的for循环本质上就是通过不断调用next()函数实现的

 

 

>>> from collections import Iterable
>>> isinstance([], Iterable)
True
>>> isinstance({}, Iterable)
True
>>> isinstance('abc', Iterable)
True
>>> isinstance((x for in range(10)), Iterable)
True
>>> isinstance(100, Iterable)
False

而生成器不但可以作用于for循环,还可以被next()函数不断调用并返回下一个值,直到最后抛出StopIteration错误表示无法继续返回下一个值了。

*可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator

可以使用isinstance()判断一个对象是否是Iterator对象:

1
2
3
4
5
6
7
8
9
>>> from collections import Iterator
>>> isinstance((x for in range(10)), Iterator)
True
>>> isinstance([], Iterator)
False
>>> isinstance({}, Iterator)
False
>>> isinstance('abc', Iterator)
False

生成器都是Iterator对象,但listdictstr虽然是Iterable,却不是Iterator

listdictstrIterable变成Iterator可以使用iter()函数:

1
2
3
4
>>> isinstance(iter([]), Iterator)
True
>>> isinstance(iter('abc'), Iterator)
True

你可能会问,为什么listdictstr等数据类型不是Iterator

这是因为Python的Iterator对象表示的是一个数据流,Iterator对象可以被next()函数调用并不断返回下一个数据,直到没有数据时抛出StopIteration错误。可以把这个数据流看做是一个有序序列,但我们却不能提前知道序列的长度,只能不断通过next()函数实现按需计算下一个数据,所以Iterator的计算是惰性的,只有在需要返回下一个数据时它才会计算。

Iterator甚至可以表示一个无限大的数据流,例如全体自然数。而使用list是永远不可能存储全体自然数的。

 

小结

凡是可作用于for循环的对象都是Iterable类型;

凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;

集合数据类型如listdictstr等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。

Python的for循环本质上就是通过不断调用next()函数实现的,例如:

1
2
for in [12345]:
    pass

实际上完全等价于:

复制代码
# 首先获得Iterator对象:
it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
    try:
        # 获得下一个值:
        x = next(it)
    except StopIteration:
        # 遇到StopIteration就退出循环
        break

转载于:https://www.cnblogs.com/linux20190409/p/10865220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值