装饰器

作者: 老夫刘某 | 来源:发表于2017-08-09 10:41 被阅读0次

先来一个简单的例子:

#!/usr/bin/env python
def log(func):    #接受被装饰函数
    def dec(*argv,**kwargs):   #接受被装饰函数的参数
        print("this is a add exaple!!")
        return func(*argv,**kwargs)  #接受被装饰函数的参数
    return dec


@log
def test(name,age):
    print("this is a test example!! %s   %s"  %  (name,age))

if __name__=="__main__":
    test("liujiangbu",27)


z装饰器带参数:

!/usr/bin/env python

encoding: utf-8

def start_info():
print ('电视剧开头曲.......')
print ('开始唱歌.......')
def end_info():
print ('电视剧结束曲.......')

def filter(start_info,end_info): #接收俩函数
def outer(main_fun): #接收装饰的函数
def app(argv,kwargs): #接收装饰的函数的参数
print('******************************')
start_info()
main_fun(
argv,**kwargs)#接收装饰的函数的参数
end_info()
print('******************************')
return app
return outer

先把函数传进去,然后在用里面的函数装饰

传函数的装饰器必须有三个def ,第一个是接受函数,第二个是装饰函数的,返回第三个函数对象

把需要装饰的函数重新定义,然后调用调用

1: filter(start_info,end_info):

2: @outer -> one_info = outer(one_info)

@filter(start_info,end_info) #这里传入俩个函数
def one_info(name,info):
print ('this is one')
print('wolcome to tv %s .......' % (name))
print('wolcome to tv %s .......' % (info))

@filter(start_info,end_info)
def two_info(name,info):
print('this is two')
print('wolcome to tv %s .......' % (name))
print('wolcome to tv %s .......' % (info))

@filter(start_info,end_info)
def three_info(name,info):
print('this is three')
print('wolcome to tv %s .......' % (name))
print('wolcome to tv %s .......' % (info))

if name == "main":
print('三国演义三部曲开始。。。。。。。。')
print('第一部。。。。。。。。。。。。。。')
one_info('三国电视剧第一部', '三国大战')

print('第二部。。。。。。。。。。。。。。')
two_info('三国电视剧第二部', '三国英雄')

print('第三部。。。。。。。。。。。。。。')
three_info('三国电视剧第三部', '三国鼎力')

装饰器不带参数:

!/usr/bin/env python

encoding: utf-8

def start_info():
print ('电视剧开头曲.......')
print ('开始唱歌.......')
def end_info():
print ('电视剧结束曲.......')

def outer(main_fun): #接收装饰的函数
def app(argv,kwargs): #接收装饰的函数的参数
print('******************************')
start_info()
main_fun(
argv,**kwargs)#接收装饰的函数的参数
end_info()
print('******************************')
return app

1: @outer -> one_info = outer(one_info)

@outer
def one_info(name,info):
print ('this is one')
print('wolcome to tv %s .......' % (name))
print('wolcome to tv %s .......' % (info))

@outer
def two_info(name,info):
print('this is two')
print('wolcome to tv %s .......' % (name))
print('wolcome to tv %s .......' % (info))

@outer
def three_info(name,info):
print('this is three')
print('wolcome to tv %s .......' % (name))
print('wolcome to tv %s .......' % (info))

if name == "main":
print ('三国演义三部曲开始。。。。。。。。')
print ('第一部。。。。。。。。。。。。。。')
one_info('三国电视剧第一部','三国大战')

print('第二部。。。。。。。。。。。。。。')
two_info('三国电视剧第二部','三国英雄')

print('第三部。。。。。。。。。。。。。。')
three_info('三国电视剧第三部','三国鼎力')

http://www.cnblogs.com/cicaday/p/python-decorator.html

相关文章

  • 装饰器

    """@装饰器- 普通装饰器- 带参数的装饰器- 通用装饰器- 装饰器装饰类- 内置装饰器- 缓存装饰器- 类实现...

  • typescript 五种装饰器

    装饰器类型 装饰器的类型有:类装饰器、访问器装饰器、属性装饰器、方法装饰器、参数装饰器,但是没有函数装饰器(fun...

  • python——装饰器详解

    一、装饰器概念 1、装饰器 装饰器:一种返回值也是一个函数的函数,即装饰器。 2、装饰器目的 装饰器的目的:装饰器...

  • Python装饰器

    Python装饰器 一、函数装饰器 1.无参装饰器 示例:日志记录装饰器 2.带参装饰器 示例: 二、类装饰器 示例:

  • Python中的装饰器

    Python中的装饰器 不带参数的装饰器 带参数的装饰器 类装饰器 functools.wraps 使用装饰器极大...

  • 装饰器

    装饰器 decorator类装饰器 带参数的装饰器 举例(装饰器函数;装饰器类;有参与无参) https://fo...

  • TypeScript装饰器

    前言 装饰器分类 类装饰器 属性装饰器 方法装饰器 参数装饰器需要在tsconfig.json中启用experim...

  • python之装饰器模版

    装饰器的作用:装饰器即可以装饰函数也可以装饰类。装饰器的原理:函数也是对象 1.定义装饰器 2.使用装饰器假设de...

  • 装饰器实验

    装饰器实验 说明 ts内包含了四个装饰器,类装饰器、属性装饰器、函数装饰器、参数装饰器,本文中测试一下其的使用。 ...

  • python3基础---详解装饰器

    1、装饰器原理 2、装饰器语法 3、装饰器执行的时间 装饰器在Python解释器执行的时候,就会进行自动装饰,并不...

网友评论

      本文标题:装饰器

      本文链接:https://www.haomeiwen.com/subject/rhsfrxtx.html