Python 装饰器解读

Python 装饰器解读

英文名: decorator

语法示例

def decorator(obj):
	print("You used a decorator.")
	return obj

@decorator
def func():
	pass

@decorator
class clazz:
	pass

可以看到, 装饰器可以应用到函数和类上.

定义

在定义时, 装饰器表现为一个可以接受1个参数的callable, 因此, 也可以用有__call__方法, 且该方法接受至少1个的参数的class实现. 这个callable返回时, 装饰器将返回一个obj, 作为新的类/函数的值.

比如, 这两种装饰器是等价的:

def function_decorator(msg):
	def wrapper(clazz):
		print('decorator')
		print('Message:', msg)
		return clazz
	return wrapper
class class_decorator:
	def __init__(self, msg):
		self.msg = msg

	def __call__(self, clazz):
		print('decorator')
		print('Message:', self.msg)
		return clazz

带参数的装饰器

>>> def a(*args, **kwargs):
		print(*args)
		def dec(clazz):
			class _A(clazz):
				def mm():
					pass
			print('hook a')
			return _A
		return dec

>>> @a(1,2)
class A:
	pass

hook a
1 2
>>> hasattr(A,'mm')
True
>>> 

以上是我在 IDLE 中的运行结果, 说明在修饰器中, 如果修饰器"含参数", 那么, 这个装饰器首先会被当做一个函数处理. 在这个例子里, 相当于先执行a(1,2), 将返回结果作为一个装饰器应用于class A上.

效果

假设我们已经有了一个如下的修饰器:

def decorator(*args, **kwargs):
	print(*args)
	def dec(clazz):
		class _A(clazz):
			def mm():
				pass
		print('hook a')
		return _A
	return dec

那么, 以下代码应该是等效的:

@decorator(1,2)
class Foo:
	pass

等效于

dec = decorator(1, 2)
@dec
class Foo:
	pass

等效于

class Foo:
	pass
dec = decorator(1,2)
Foo = dec(Foo)

这三段代码得到的Foo类应该完全一样

多层装饰器

假设有两个装饰器deco1deco2, 如果这么用:

@deco1
@deco2
def func():
	pass

那么等价于

func = deco1(deco2(func))

装饰器用途

链接为 Github 链接或 Python 官方文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This module provides the following functionality... * 'C' style interface The Tuxedo perl module gives you access to almost all of the tuxedo 8.1 apis from perl. In most cases you can take the C API you already familiar with, apply perl semantics to it, and write working tuxedo programs in perl. * Object wrapping of C structures Many tuxedo functions take pointers to C structures as function parameters. To preserve the C interface, this module provides perl objects that encapsulate the C structures used by tuxedo. These objects allow the user to create and manipulate the elements of these C structures, and these objects are then passed as parameters to the perl version of these tuxedo C functions. * buffer management Perl classes exist for each buffer type to allow for easy manipulation of buffer contents and automatic memory cleanup when no more references to the buffer exist. * callback subs perl subs can be registered as unsolicited message handlers and signal handlers. * FML/FML32 field table support This module includes the mkfldpm32.pl script that is the perl equivalent of the tuxedo mkfldhdr32 program. It accepts a field table file as input and produces a *.pm file that can be included in a perl script, so field identifiers can be referenced by id. * perl tuxedo services You can now write tuxedo services in perl. When you build the Tuxedo module, it should create a tuxedo server called PERLSVR. This is a tuxedo server that contains an embedded perl interpretor for executing perl tuxedo services. When PERLSVR boots up, it parses the perlsvr.pl script, which at the moment it expects to find in its working directory. The location of perlsvr.pl will be configurable in a future version. The perlsvr.pl script is run as the tpsvrinit routine. You can modify perlsvr.pl to define any subs you want to be tuxedo services and advertise these subs.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值