首页 技术 正文
技术 2022年11月21日
0 收藏 472 点赞 3,334 浏览 2651 个字

一、装饰器无参数

1.原函数无参数

def wrap_in_tag_b(fn): # wrap_in_tag_b 是真正的装饰器
def wrapped():
return "<b>" + fn() + "</b>"
return wrappeddef wrap_in_tag_i(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped@wrap_in_tag_b
@wrap_in_tag_i
def hello():
return "hello"print(hello()) # returns "<b><i>hello world</i></b>"

2.原函数带参数

def wrap_in_tag_b(fn):
def wrapped(arg): # arg 是原函数参数
return "<b>" + fn(arg) + "</b>"
return wrappeddef wrap_in_tag_i(fn):
def wrapped(arg): # arg 是原函数参数
return "<i>" + fn(arg) + "</i>"
return wrapped@wrap_in_tag_b
@wrap_in_tag_i
def hello(name):
return "hello {}".formar(name)print(hello('Jack'))

二、装饰器带参数

1.原函数无参数

def wrap_in_tag(deco_arg):
# 装饰器参数:deco_arg
# 可以在任意位置使用
def real_decorator(func): # real_decorator才是即将返回的真正的装饰器
    # 原函数参数:func_arg
    # 只可以在此位置使用
    def wrapped() :
return "<{0}>{1}</{0}>".format(deco_arg, func())
return wrapped
return real_decorator@wrap_in_tag('b')
@wrap_in_tag('i')
def hello():
return "hello"print(hello())

2.原函数带参数

def wrap_in_tag(deco_arg):
# 装饰器参数:deco_arg
# 可以在任意位置使用
def real_decorator(func): # real_decorator才是即将返回的真正的装饰器
def wrapped(func_arg):
# 原函数参数:func_arg
# 只可以在此位置使用
return "<{0}>{1}</{0}>".format(deco_arg, func(func_arg))
return wrapped
return real_decorator@wrap_in_tag('b')
@wrap_in_tag('i')
def hello(name):
return "hello {}".format(name)print(hello('Jack'))

三、装饰器类

1.原函数无参数

class wrap_in_tag(object):
def __init__(self, deco_arg):
self.tag = deco_arg # 装饰器参数:deco_arg def __call__(self, func):
def newf(): # 原函数无参数
return "<{0}>{1}</{0}>".format(self.tag, func())
return newf@wrap_in_tag('b')
@wrap_in_tag('i')
def hello():
return 'hello'print(hello())

2.原函数带参数

class wrap_in_tag(object):
def __init__(self, deco_arg):
self.tag = deco_arg # 装饰器参数:deco_arg def __call__(self, func):
def newf(func_arg): # 原函数参数: func_arg
return "<{0}>{1}</{0}>".format(self.tag, func(func_arg))
return newf@wrap_in_tag('b')
@wrap_in_tag('i')
def hello(name):
return "hello {}".format(name)print(hello('Jack'))

四、用装饰器装饰类

1.用函数作为装饰器

def wrap_in_tag(deco_arg):
def real_decorator(func):
def wrapped(self, func_arg): # 类方法接收第一个参数都是self,这个必须有,而无论func_arg是否有!!!
return "<{0}>{1}</{0}>".format(deco_arg, func(func_arg))
return wrapped
return real_decoratorclass foo(object): @wrap_in_tag('b')
@wrap_in_tag('i')
def hello(self, name):
return 'hello {}".format(name) @wrap_in_tag('b')
@wrap_in_tag('i')
def byebye(self, name):
return 'byebye {}".format(name)f = foo()
print(f.hello('Jack')

2.用类作为装饰器

class wrap_in_tag(object):
def __init__(self, deco_arg):
self.tag = deco_arg def __call__(self, func):
def newf(slf, func_arg): # slf必须!self被占用了,那就给第一个参数另一个名字slf(随便)
return "<{0}>{1}</{0}>".format(self.tag, func(func_arg))
return newfclass foo(object): @wrap_in_tag('b')
@wrap_in_tag('i')
def hello(self, name):
return 'hello {}".format(name) @wrap_in_tag('b')
@wrap_in_tag('i')
def byebye(self, name):
return 'byebye {}".format(name)
f = foo()
print(f.hello('Jack'))
 
上一篇: Subsonic 介绍
下一篇: CF97C Winning Strategy
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894