装饰 [英] Decorator

查看:34
本文介绍了装饰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 2.3。

我听说过Python 2.4中的装饰器。

什么是装饰器有用?

谢谢你回复

L.

I use Python 2.3.
I have heard about decorators in Python 2.4.
What is the decorator useful for?
Thanks for reply
L.

推荐答案

Lad启发我们:
I使用Python 2.3。
我听说过Python 2.4中的装饰器。
什么是装饰器有用的?
I use Python 2.3.
I have heard about decorators in Python 2.4.
What is the decorator useful for?




很多东西。我已将它们用于:

- 记录对函数的所有调用,包括其参数。

- 在函数

调用。

- 在将参数传递给某些类型之前将它们传递给

函数。


And还有更多可能...


Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa



A whole lot of stuff. I''ve used them for:
- Logging all calls to a function, including its arguments.
- Ensuring there is a database connection before the function is
called.
- Casting the arguments to certain types before passing them to
the function.

And there is much more possible...

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


" Lad" < PY **** @ hope.cz>在消息中写道

新闻:11 ********************** @ i39g2000cwa.googlegr oups.com ...
"Lad" <py****@hope.cz> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
我使用Python 2.3。
我听说过Python 2.4中的装饰器。
装饰器对什么有用?
感谢回复
L。
I use Python 2.3.
I have heard about decorators in Python 2.4.
What is the decorator useful for?
Thanks for reply
L.



在Python维基上查看这些示例:
http://wiki.python.org/moin/PythonDecoratorLibrary


我必须成为备忘录的忠实粉丝。

- Paul


Check out these examples on the Python wiki:
http://wiki.python.org/moin/PythonDecoratorLibrary

I''ve gotten to be a big fan of memoize.

-- Paul


Lad写道:
我使用Python 2.3。
我听说过Python中的装饰器2.4。


Python 2.4添加的只是装饰器的语法糖。您可以在2.3中同样地使用
- 更明确一点 -

装饰器有用的是什么?
I use Python 2.3.
I have heard about decorators in Python 2.4.
What Python 2.4 adds is only syntactic sugar for decorators. You can do
the same - somewhat more explicitely - in 2.3.
What is the decorator useful for?




FWIW,我不确定''装饰'这个名字是个好主意。一个

装饰器(读''函数装饰器'')主要是一个可调用的,它可以作为param调用

并返回另一个可调用的,通常包含

通过了可调用并添加了一些响应性(跟踪,记录,

事后条件检查等)。两个着名的例子是classmethod

和staticmethod。


整个事情看起来像这样:


def deco(func):

print" decorating%s" %func .__ name__

def _wrapper(* args,** kw):

print"%s called" %func .__ name__

res = func(* args,** kw)

print"%s return%S" %(func .__ name __,str(res))

return _wrapper


#python< 2.4

def somefunc():

print" somefunc"

return 42


somefunc = deco(somefunc)


2.4中添加的语法糖允许你避免显式调用

来deco():


#python> = 2.4

@deco

def someotherfunc():

返回鹦鹉死了 ;

如你所见,除了语法糖之外,这里没有什么新东西。

它只是普通的高阶函数,在任何一个都是众所周知的功能性

语言。


HTH

-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: - 1] for w in p.split(''。'')])for ''o **** @ xiludom.gro''中的
p.split(''@'')])"



FWIW, I''m not sure the name ''decorator'' is such a great idea. A
decorator (read ''function decorator'') is mainly a callable that takes a
callable as param and returns another callable, usually wrapping the
passed callable and adding some responsabilities (tracing, logging,
pre-post condition checks, etc). Two well-known examples are classmethod
and staticmethod.

The whole things looks like this:

def deco(func):
print "decorating %s" % func.__name__
def _wrapper(*args, **kw):
print "%s called " % func.__name__
res = func(*args, **kw)
print "%s returned %s" % (func.__name__, str(res))
return _wrapper

# python < 2.4
def somefunc():
print "in somefunc"
return 42

somefunc = deco(somefunc)

The syntactic sugar added in 2.4 allows you to avoid the explicit call
to deco():

# python >= 2.4
@deco
def someotherfunc():
return "the parrot is dead"
As you see, apart from the syntactic sugar, there''s nothing new here.
It''s just plain old higher order function, well known in any functional
language.

HTH
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


这篇关于装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆