问题描述
ISTM
@limited_expression_producing_function
@another
def func():传递
是用于创建隐藏的函数列表的语法糖。 (使用''|'代替'''''
并没有太大的改变(除了那些工具依赖于''@''的人; - )。 br />
即,(没有来源或时间去钻研)上面
的表观语义大致类似于
__funclist__ = []
__funclist __。append(limited_expression_producing_f unction)
__funclist __。追加(另一个)
def func():传递
而__funclist__:func = __funclist __。pop()(func)
del __funclist__
这是一个特例吗?更一般的想法?例如,在任何下一个名字绑定后,它是否适用于
,一般而言,不仅仅是由def绑定的名称?
因此(未经测试)
def censor(cls):
cls .__ repr__ = lambda self:''< CENSORED>''
返回cls
...
@censor
class C(对象):传递
可能有预期的效果(在元类效应之后,如果有的话,大概是BTW)
(请注意,审查员可以改为包裹选定的方法或添加类变量数据等,
虽然IIRC __metaclass__可以创建一些以后只读的东西。
这仍然是由前缀上下文非常狭窄地定义的。这个上下文还是
特殊情况下默认的更普遍吗? IOW默认选择
命名空间是词法封闭的。那么,例如,能够在模块顶部的一个地方指定
装饰并装饰(以相同的方式,使用
相同的功能列表)所有指定(按名称)类列表的方法?
即,当什么名称绑定在什么名称空间中时,更一般地控制该怎么做
可以设想。这开始感觉像事件驱动的处理。 @ deco1 @ deco2
可以用于特殊情况的糖(不要用字面意思,只是说明语义;-)
when(event = ''next_binding'',namespace =''immediate'',symbols =''any'',funclist =(deco1,deco2))
def foo():传递
一般的东西,也可能包括其他和可能重复的事件,如完成
的arg列表,用于调用特定函数或方法或任何重新绑定特定
指定命名空间中的
符号(不仅仅是直接命名空间),例如,用于调试或分析等等。
因为我将它复制到python-dev,我''将再次询问@decorator是否有任何特殊的
线程安全陷阱应该加以警告,以防万一。
请将此作为探测预期的语义,而不是任何特定
功能的提议;-)
问候,
Bengt R ichter
ISTM that
@limited_expression_producing_function
@another
def func(): pass
is syntactic sugar for creating a hidden list of functions. (Using ''|'' in place of ''@''
doesn''t change the picture much (except for people whose tools depend on ''@'' ;-)).
I.e., (not having the source or time to delve) the apparent semantics of the above
is something roughly like
__funclist__ = []
__funclist__.append(limited_expression_producing_f unction)
__funclist__.append(another)
def func():pass
while __funclist__: func = __funclist__.pop()(func)
del __funclist__
Is this a special case of a more general idea? E.g., could it apply to
right after ANY next name is bound, in general, not just a name bound by def?
thus (untested)
def censor(cls):
cls.__repr__ = lambda self: ''<CENSORED>''
return cls
...
@censor
class C(object): pass
could have the expected effect (after metaclass effects, if any, presumably, BTW)
(note that censor could instead e.g. wrap selected methods or add class variable data etc.,
though IIRC __metaclass__ can create some things that are read-only later)
This is still very narrowly defined by prefix context. Is this context also
a special case default of something more general? IOW the default choice for
namespace is the lexically enclosing one. What about, e.g., being able to specify
decoration in one place at the top of a module and decorate (in the same way, using
the same function list) all methods of a specified (by name) list of classes?
I.e., a more general control over what to do when what names are bound in what namespace
could be envisaged. This begins to feel like event-driven processing. Could @deco1 @deco2
be sugar for the special case (don''t take this literally, just illustrating semantics ;-)
when(event=''next_binding'', namespace=''immediate'', symbols=''any'', funclist=(deco1,deco2))
def foo(): pass
of something general that could also include other and possibly repeated events like on completion
of an arg list for a particular function or method being called or any rebinding of particular
symbols in a specified namespace (not just the immediate one), e.g., for debugging or profiling etc.
Since I am copying this to python-dev, I''ll re-ask whether @decorator has any peculiar
thread safety pitfalls that should be warned against, just in case.
Please take this as a probe into the intended semantics, not as a proposal for any particular
functionality ;-)
Regards,
Bengt Richter
推荐答案
相当于:
def func():传递
func = limited_expression_producing_function(另一个(func))
-
mithrandi,i Ainil en-Balandor,faer Ambar
----- BEGIN PGP SIGNATURE -----
版本:GnuPG v1.2.5(GNU / Linux)
iD8DBQFBFW0opNuXDQIV94oRAljjAJ0aJsggvp2vEVeCgBCapi TgbSb + gQCfTWTF
OUghKvwWu / GZ7JMTBf9ppzI =
= BwLb
-----结束PGP SIGNATURE -----
is equivalent to:
def func(): pass
func = limited_expression_producing_function(another(func ))
--
mithrandi, i Ainil en-Balandor, a faer Ambar
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
iD8DBQFBFW0opNuXDQIV94oRAljjAJ0aJsggvp2vEVeCgBCapi TgbSb+gQCfTWTF
OUghKvwWu/GZ7JMTBf9ppzI=
=BwLb
-----END PGP SIGNATURE-----
嗯,即便如此,还有很多值得喜欢的东西,但我不想放弃我们现在拥有的东西 - 你现在可以用更实用的方式做事,
$ b带嵌套作用域的$ b。你真的想回到三次打击
并且你在寻找符号吗?这种语言已经发生了很好的变化,并且(非常令人惊讶地)变得更加一致的模式(语言设计中的b
主要政变)。推动
变化的美学表明这可能是声明者的正确语法。
我知道我不会做几乎和Guido和帮派一样好制作
语言决定。
我当然打算把这段阿尔法时期作为一个时间用装饰器来实验
并看看如果语法在我身上增长;我建议别人
做同样的事情。给它最好的机会;你可能成为粉丝。几个
的py-dev人声称他们已经从厌恶变为
支持。
-
-Scott David Daniels
这篇关于@decorator语法是糖,但究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!