码迷,mamicode.com
首页 > 编程语言 > 详细

第四周 day4 python学习笔记

时间:2017-10-06 17:31:53      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:ima   images   order   png   ora   open   lock   win   打印   

  关于装饰器的更多信息可以参考http://egon09.blog.51cto.com/9161406/1836763

  1.装饰器Decorator

技术分享装饰器:本质上是函数,(装饰其他函数),就是为其他函数添加附加功能
原则:不能修改被装饰函数的源代码;不能修改被装饰函数的调用方式
#实现装饰器的知识储备:
1.函数即变量
2.高阶函数,有两种方式:
(1)把一个函数名当做实参传递给另一个函数(在不修改被装饰函数源代码的情况下为其添加功能)
(2)返回值中包含函数名(不修改函数调用的方式)
3.嵌套函数
高阶函数+嵌套函数==》装饰器
技术分享import time
#计算一个函数的运行时间的装饰器
def timer(func):
    def wrapper(*kargs,**kwargs):
        start_time=time.time()
        func()
        end_time=time.time()
        print("the func runtime is %s"%(end_time-start_time))
    return wrapper

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

test1()
技术分享#高阶函数
def bar():
    print("in the bar...")

def test(func):
    print(func)#打印出该函数变量的地址
    func()

test(bar)

技术分享# 把一个函数名当做实参传递给另一个函数
import time
def bar():
    time.sleep(2)
    print("in the bar...")

def test(func):
    start_time=time.time()
    func()  #run bar()
    end_time=time.time()
    print(" the func runtime is %s"%(end_time-start_time))

test(bar)
技术分享# 返回值中包含函数名
import time
def bar():
    time.sleep(2)
    print("in the bar...")

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

bar2=test2(bar)
bar2()
技术分享#嵌套函数
#局部作用域和全局作用域的访问顺序
x=0
def grandpa():
    x=1
    print("grandpa:",x)
    def dad():
        x=2
        print("dad:",x)
        def son():
            x=3
            print("son:",x)
        son()
    dad()
grandpa()

#匿名函数:没有定义函数名字的函数
calc=lambda x,y:x+y
print(calc(13,15))
技术分享import time
def timer(func):
    def deco(*kargs,**kwargs):
        start_time=time.time()
        func(*kargs,**kwargs)
        end_time=time.time()
        print(" the func runtime is %s"%(end_time-start_time))
    return deco

@timer  # test1=timer(test1)
def test1():
    time.sleep(2)
    print("in the test1.....")

# test1=timer(test1)
test1()

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

test2("Jean_V",20)

技术分享#装饰器进阶版
#对各个页面添加用户认证
username,password="admin","123"
def auth(auth_type):
    print("auth_type:",auth_type)
    def out_wrapper(func):
        def wrapper(*args,**kwargs):
            if auth_type=="local":
                uname=input("请输入用户名:").strip()
                passwd=input("请输入密码:").strip()
                if uname==username and passwd==password:
                    print("\033[32;1m User has passed authentication\033[0m")
                    res=func(*args,**kwargs)
                    print("************after authentication")
                    print(res)
                else:
                    exit("\033[31;1m Invalid username or password\033[0m")
            elif auth_type=="LADP":
                print("我不会LADP认证,咋办?。。。")
        return wrapper
    return out_wrapper

def index():
    print("index page......")

@auth(auth_type="local")#home 页采用本地验证
def home():
    print("home page.....")
    return "from home page....."

@auth(auth_type="LADP")#bbs 页采用LADP验证
def bbs():
    print("bbs page....")

index()
print(home())
home()
bbs()










第四周 day4 python学习笔记

标签:ima   images   order   png   ora   open   lock   win   打印   

原文地址:http://www.cnblogs.com/jean925/p/7631797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!