博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python学习day4之路
阅读量:6944 次
发布时间:2019-06-27

本文共 5508 字,大约阅读时间需要 18 分钟。

装饰器(http://egon09.blog.51cto.com/9161406/1836763)

1、装饰器:本质是函数;

装饰器(装饰其他函数),就是为其他函数添加附加功能;

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

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

装饰器对被装饰的函数完全透明的,没有修改被装饰函数的代码和调用方式。

实现装饰器知识储备:

1.函数即“变量”;

2.高阶函数;

3.嵌套函数

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

匿名函数(lambda表达式)

>>> calc = lambda x:x*3

>>> calc(2)
6

高阶函数:

  a.把一个函数名当做实参传递给另外一个函数;

>>> def bar():

  print("in the bar.....")  

>>> def foo(func):

   print(func)

>>> foo(bar)

<function bar at 0x7f8b3653cbf8>  

  b.返回值中包含函数名;

>>> import time

>>> def foo():

  time.sleep(3)
  print("in the foo.....")
>>> def main(func):
   print(func)
   return func
>>> t = main(foo)
<function foo at 0x7fb7dc9e3378>
>>> t()
in the foo.....

 

装饰器:

在不修改源代码的情况下,统计程序运行的时间:

 

import time def timmer(func):     def warpper(*args,**kwargs):   #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    #等价于test1 = timmer(test1),因此函数的执行调用是在装饰器里面执行调用的 def test1():     time.sleep(3)     print("in the test1") test1() 运行结果如下:

in the test1

the func run time is 3.001983404159546

装饰器带参数的情况:

 

import time def timmer(func):     def warpper(*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 warpper    #返回内层函数名 @timmer def test1():     time.sleep(3)     print("in the test1") @timmer    #test2 = timmer(test2) def test2(name):     print("in the test2 %s" %name) test1() test2("alex") 运行结果如下:

in the test1

the func run time is 3.0032410621643066
in the test2 alex
the func run time is 2.3603439331054688e-05

装饰器返回值情况:

 

import timeuser,passwd = "alex","abc123"def auth(func):    def wrapper(*args,**kwargs):        username = input("Username:").strip()        password = input("Password:").strip()        if user == username and passwd == password:            print("\033[32;1mUser has passed authentication.\033[0m")            return func(*args,**kwargs)   #实际上执行调用的函数               # res = func(*args,**kwargs)            # return res   #函数返回值的情况,因为装饰器调用的时候是在装饰器调用的函数,因此返回值也在这个函数中        else:            exit("\033[31;1mInvalid username or password.\033[0m")    return wrapperdef index():    print("welcome to index page...")@authdef home():    #用户自己页面    print("welcome to home page...")    return "form home..."@authdef bbs():    print("welcome to bbs page")index()print(home())bbs()

 

装饰器带参数的情况:

实现:1、本地验证;2、远程验证

import timeuser,passwd = "alex","abc123"def auth(auth_type):    '''函数的多层嵌套,先执行外层函数'''    print("auth_type",auth_type)    def out_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")                    func(*args,**kwargs)   #实际上执行调用的函数                    # res = func(*args,**kwargs)                    # return res   #函数返回值的情况,因为装饰器调用的时候是在装饰器调用的函数,因此返回值也在这个函数中                else:                    exit("\033[31;1mInvalid username or password.\033[0m")            elif auth_type == "ldap":                print("搞毛线lbap,傻逼....")        return wrapper    return out_wrapperdef index():    print("welcome to index page...")@auth(auth_type="local")def home():    #用户自己页面    print("welcome to home page...")    return "form home..."@auth(auth_type="ldap")def bbs():    print("welcome to bbs page")index()home()bbs()   #函数没有,因为没有调用函数,函数的调用在装饰器里面,是装饰器调用了函数

   迭代器和生成器

  生成器

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

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

  >>> l1 = (i for i in range(10))

  >>> l1
  <generator object <genexpr> at 0x7f6a9fbcaeb8>
  >>> l1.__next__()
  0
  >>> l1.__next__()
  1
  生成器:只有在调用时才会生成相应的数据;

  只有通过__next__()方法进行执行,这种能够记录程序运行的状态,yield用来生成迭代器函数。(只能往后调用,不能向前或者往后推移,只记住当前状态,因此银行的系统用来记录的时候可以使用yield函数)。

'''利用yield实现异步的效果,发送接收消息'''import timedef consumer(name):    '''消费者吃包子模型'''    print("%s准备吃包子了......" %name)    while True:        '''循环,由于没有终止'''        baozi = yield        print("包子%s被%s吃了......" %(baozi,name))def producer(boss):    '''生产者生产包子模型,生产者要生产包子'''    c1 = consumer("A")    c2 = consumer("B")    c1.__next__()    c2.__next__()    '''接下来,生产者要生产包子了,并传递给消费者'''    for i in range(1,10):        time.sleep(1)        c1.send(i)        c2.send(i)producer("Alex") 运行如下: A准备吃包子了...... B准备吃包子了...... 包子1被A吃了...... 包子1被B吃了...... 包子2被A吃了...... 包子2被B吃了...... 包子3被A吃了...... 包子3被B吃了...... 包子4被A吃了...... 包子4被B吃了...... 包子5被A吃了...... 包子5被B吃了...... 包子6被A吃了...... 包子6被B吃了...... 包子7被A吃了...... 包子7被B吃了...... 包子8被A吃了...... 包子8被B吃了...... 包子9被A吃了...... 包子9被B吃了.....

  迭代器

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

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

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

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

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

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

 

 

 

 

 

 

转载于:https://www.cnblogs.com/gengcx/p/7107728.html

你可能感兴趣的文章
ORALCE存储之ROWID
查看>>
[php]php设计模式 Composite (组合模式)
查看>>
VBA之四----给程序自动加行号
查看>>
Windows 下 Nginx + PHP5 的安装与配置
查看>>
【技术贴】所有好友的QQ空间都打不开进不去的超简单解决办法!
查看>>
这种写法用过没:string.Format("{0,-10}", 8)
查看>>
有关在SharePoint Server中Infopath表单无法呈现的问题及解决方案
查看>>
HDU-1572 下沙小面的(2) DFS
查看>>
Silverlight3.0正式版(Silverlight3_Tools)离线安装
查看>>
微博营销,究竟该怎么做?(实战系列四:活动篇)
查看>>
Sharepoint学习笔记—Ribbon系列-- 1. Ribbon的架构
查看>>
交换机与路由器的区别
查看>>
对${ZSH_VERSION+set}的验证
查看>>
Struts2和Spring3 MVC的区别说明
查看>>
掌握这些电脑知识,你会玩得很无耻
查看>>
admob 广告增加
查看>>
客户/服务器程序设计范式
查看>>
由一个Xml序列化操作看mscorlib.dll 2.0、4.0 String的Trim方法实现
查看>>
Solr拼写检查(spellCheck)配置和使用
查看>>
javascript 中cookie的存储,获取cookie,删除cookie的方法
查看>>