__metaclass__和__author__已经是装饰者 [英] __metaclass__ and __author__ are already decorators

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

问题描述

考虑装饰者,看看我们在我们的Python代码中已经做了什么,似乎__metaclass __,__ author __,__ version__,

等都是例子装饰者所以我们已经有一个装饰器

语法。发明新产品的理由是什么?如果我们

做什么,那么旧的是什么?


这是我对此的看法。我们有两种装饰器:

仅供参考(如__author__和__version__),以及那些有副作用的
,即那些实际上*做*的装饰某事(比如

__metaclass__)。


class Foo:

"""这将Foo类描述为正常。 ""

__metaclass__ = M

__author__ =''Paul Morrow''

__version__ =''0.1''

__automethods__ = True

def baz(self,a,b):

"""这描述了baz方法。 ""

__synchronized__ = True

__returns__ =无

__author__ =''Neville Shunt''

#巴兹的身体在这里...


那里,看起来非常清晰和pythonic。现在如何定义

装饰器。

def metaclass(decoratedClass,subType):

"""这描述了''元类''装饰器。 ""

__decorator__ = True

__version__ =''1.9''

#对decoratedClass执行元类操作
def synchronized(decoratedFunc,trueOrFalse):

"""这描述了''synchronized''装饰器。 ""

__decorator__ = True

__author__ =''Martin Curry''

__version__ =''0.5''

#对decoratedClass执行同步操作

def return(decoratedFunc,* args):

""" Docstring for''returns' 'decorator。'""

__decorator__ = True

#装饰者的身体到这里

每个装饰器函数接收类|函数|方法是

作为函数的第一个参数进行装饰(例如,DecorativeClass

和上面的decoratedFunc)。其余参数是在装饰的

函数的定义中分配给__xxx__变量的值




所以,在上面的例子,当''返回''装饰器函数被调用来装饰''baz''方法时,得到了装饰函数,装饰函数将接收baz

对象和args [ 0]将设置为None(因为baz定义中的声明

" __ returns__ = None。


对于函数是用作装饰器功能,它必须通过将__decorator__属性设置为True来装饰




这是否足以处理装饰器问题?


Paul

Thinking about decorators, and looking at what we are already doing in
our Python code, it seems that __metaclass__, __author__, __version__,
etc. are all examples of decorators. So we already have a decorator
syntax. What is the compelling reason to invent a new one? And if we
do, what''s to become of the old one?

Here''s my take on this. We have two kinds of decorators: those that
are informational only (like __author__ and __version__), and those
which have side-effects, i.e. those that actually *do* something (like
__metaclass__).

class Foo:
""" This describes the Foo class as normal. """
__metaclass__ = M
__author__ = ''Paul Morrow''
__version__ = ''0.1''
__automethods__ = True
def baz(self, a, b):
""" This describes the baz method. """
__synchronized__ = True
__returns__ = None
__author__ = ''Neville Shunt''
# body of baz goes here...

There, that looks pretty clear and pythonic. Now how to define the
decorators.

def metaclass(decoratedClass, subType):
""" This describes the ''metaclass'' decorator. """
__decorator__ = True
__version__ = ''1.9''
# perform the metaclass operation on decoratedClass
def synchronized(decoratedFunc, trueOrFalse):
""" This describes the ''synchronized'' decorator. """
__decorator__ = True
__author__ = ''Martin Curry''
__version__ = ''0.5''
# perform the synchronized operation on decoratedClass
def returns(decoratedFunc, *args):
"""Docstring for ''returns'' decorator."""
__decorator__ = True
# body of decorator goes here
Each decorator function receives the class|function|method being
decorated as the first parameter of the function (e.g. decoratedClass
and decoratedFunc above). The remaining parameters are the values
assigned to the __xxx__ variable in the definition of the decorated
function.

So, in the above example, when the ''returns'' decorator function is
called to decorate the ''baz'' method, decoratedFunc would recieve the baz
object and args[0] would be set to None (because of the statement
"__returns__ = None" in the definition of baz).

For a function to be used as a decorator function, it must be decorated
as such, by setting its __decorator__ attribute to True.

Does this handle enough of the decorator concerns?

Paul

推荐答案

Paul Morrow写道:
Paul Morrow wrote:
考虑装饰器,看看我们在Python代码中已经在做什么,似乎__metaclass __,__ author__,__ version__,
等等。重新装饰的所有例子。所以我们已经有了一个装饰器
语法。发明新产品的理由是什么?如果我们这样做,那么旧的会变成什么?
< snip>
Thinking about decorators, and looking at what we are already doing in
our Python code, it seems that __metaclass__, __author__, __version__,
etc. are all examples of decorators. So we already have a decorator
syntax. What is the compelling reason to invent a new one? And if we
do, what''s to become of the old one?
<snip>




我想过在你的建议之前的一些事情,

但是,对于我来说,赋予特殊含义的特殊含义并不是很好的Pythonic

变量。它本质上是一种新的语法,但是

伪装成一种具有完全不同含义的旧语法。



I''ve thought about something along the lines of your suggestion before,
but it doesn''t seem very Pythonic to assign special meaning to function
variables with special names to me. It''s essentially a new syntax, but
disguised as an old syntax with an entirely different meaning.


Leif K-Brooks写道:
Leif K-Brooks wrote:
Paul Morrow写道:
Paul Morrow wrote:
思考装饰器,看看我们在Python代码中已经做了些什么,它似乎__metaclass __,__ author__,__ version__,
等都是装饰器的例子。所以我们已经有了一个装饰器
语法。发明新产品的理由是什么?如果我们这样做,那么旧的会变成什么?
< snip>
Thinking about decorators, and looking at what we are already doing in
our Python code, it seems that __metaclass__, __author__, __version__,
etc. are all examples of decorators. So we already have a decorator
syntax. What is the compelling reason to invent a new one? And if we
do, what''s to become of the old one?
<snip>



我已经想到了一些事情之前你的建议,但是对我来说,赋予具有特殊名称的变量赋予特殊含义并不是非常Pythonic。它本质上是一种新的语法,但
伪装成一种具有完全不同含义的旧语法。


I''ve thought about something along the lines of your suggestion before,
but it doesn''t seem very Pythonic to assign special meaning to function
variables with special names to me. It''s essentially a new syntax, but
disguised as an old syntax with an entirely different meaning.




我们只需要停止将它们视为本地函数变量。

相反,我们应该将__xxx__属性视为描述函数

本身(即作为装饰者),因为我相信几乎总是

作者在

函数/方法定义中使用此类名称时的意图。他想说一下

函数(谁写的,它的版本等),并且可能很难过它

有副作用创建局部变量。所以很可能

不应再有副作用了。它应该创建一个函数

属性(不要与局部变量混淆)。


对于类,它更容易接受这个看待

__xxx__属性的新方式因为它与我们的意图一致

它们(作为元数据的名称而不是正常的,可继承的类

属性)。



We just need to stop thinking of them as local function variables.
Instead we should think of __xxx__ attributes as describing the function
itself (i.e. as a decorator would), as I believe that is almost always
the author''s intention when he/she uses such names inside of a
function/method definition. He wants to say something about the
function (who wrote it, it''s version, etc.), and is probably sad that it
has the side-effect of creating a local variable. So it probably
shouldn''t have that side-effect anymore. It should create a function
attribute instead (not to be confused with a local variable).

For classes, it''s much easier to accept this new way of looking at
__xxx__ attributes because it''s consistent with our intention behind
them (as names for metadata rather than normal, inheritable class
attributes).


Paul Morrow写道:
Paul Morrow wrote:
我们只需要停止将它们视为局部函数变量。
相反,我们应该将__xxx__属性视为描述函数本身(即作为装饰者),因为我相信当他/她使用时几乎总是作者的意图
函数/方法定义中的这些名称。他想说一下
函数(谁编写它,它的版本等),并且可能很遗憾它具有创建局部变量的副作用。所以它可能不再具有那种副作用了。它应该创建一个函数
属性(不要与局部变量混淆)。
We just need to stop thinking of them as local function variables.
Instead we should think of __xxx__ attributes as describing the function
itself (i.e. as a decorator would), as I believe that is almost always
the author''s intention when he/she uses such names inside of a
function/method definition. He wants to say something about the
function (who wrote it, it''s version, etc.), and is probably sad that it
has the side-effect of creating a local variable. So it probably
shouldn''t have that side-effect anymore. It should create a function
attribute instead (not to be confused with a local variable).




就像我说的那样,赋予一个新的含义旧语法。那个

似乎让我感到困惑;为什么不只是创建一个新的语法?



Which is, like I said, assigning new meaning to an old syntax. That
seems confusing to me; why not just create a new syntax?


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

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