__decorated__为Python装饰 [英] __decorated__ for python decorators
问题描述
由于2.4(2.6类),Python允许你来装点使用其他功能的功能:
As of 2.4 (2.6 for classes), python allows you to decorate a function with another function:
def d(func): return func
@d
def test(first): pass
这是一个方便的语法糖。你可以做各种装饰用的东西整齐而不做一个烂摊子。但是,如果你想找出得到装饰你赴汤蹈火原有的功能(如 Cls.method .__ FUNC __.__封闭__ [0] .cell_contents
或更糟)
我发现自己希望一个更好的方式,发现曾有过关于添加了一个名为变量有关python-dev的一些讨论 __装饰__
来返回的[新]功能装饰。然而,似乎没有去任何地方。
I found myself wishing for a better way and found that there had been some discussion on python-dev about adding a variable called __decorated__
to the [new] function returned by the decorator. However, it appears that didn't go anywhere.
作为一个敢于冒险的人,以及有过了大约4年pretty重蟒蛇的经验,我想我会考虑在Python编译器的源实施 __装饰__
,只是为了看看是怎么回事。
Being an adventuresome person, and having had pretty heavy python experience for about 4 years, I thought I would look into implementing __decorated__
in the python compiler source, just to see how it goes.
说实话,我从来没有钻研的引擎盖下的C,所以我的第一个小时,一直只是试图让底层C code ++工程的意义。所以,首先什么是最好的资源来让我的头围绕什么,我将不得不改变/添加对 __装饰__
?
To be honest I have never delved into the C underneath the hood, so my first hours have been just trying to make sense of how the underlying C code works. So firstly, what would be the best resources to get my head around what I would have to change/add for __decorator__
?
其次,如果一个装饰返回一个新的功能,那么 __ __装饰
将只返回原始的,装饰的功能。但是,如果装饰返回原有的功能,会发生什么?下面是我能想到的三个选项(第三个是我的最爱):
Secondly, if a decorator returns a new function then __decorated__
would just return the original, decorated function. However, if the decorator returns the original function, what should happen? Here are three options I could think of (the third is my favorite):
- 请不要添加
__ __装饰
。 - 添加
__ __装饰
,但它设置为无。 - 添加
__ __装饰
并将其设置为原有的功能呢。
- Don't add
__decorator__
. - Add
__decorator__
but set it to None. - Add
__decorator__
and set it to the original function anyway.
因此,如果发生这种情况,你觉得什么是最好的选择吗?
So if it were to happen, what do you think would be the best option?
更新:
别人<一个href=\"http://stackoverflow.com/questions/3375573/finding-a-functions-parameters-in-python/3501316#3501316\">brought我注意了的我错过了一个场景。当装饰既不返回原有的功能,也不是包装了原来的功能,会发生什么?在这一点上没有什么是抱着原来的函数的引用,它会得到垃圾收集。 (感谢 Oddthinking !)
Someone else brought to my attention a scenario that I had missed. What happens when the decorator returns neither the original function nor a function that wraps the original? At that point nothing is holding a reference to the original function and it will get garbage collected. (Thanks Oddthinking!)
因此,在这种情况下,我认为我会仍与第三个选项去。由装饰返回的对象将获得引用原函数__ __装饰
名称。这意味着它不会被垃圾回收。
So in that case, I think that I would still go with the third option. The object returned by the decorator would gain a __decorated__
name that references the original function. This would mean that it would not be garbage-collected.
这似乎不可思议,我认为从一个类定义的函数将完全消失,因为你装饰它。在我的脑海里,甚至是更有理由有一个 __ __装饰
属性适用于每一个装饰。然而,它更可能是我的直觉是错误的,而目前的行为是大多数人所期望的。 有什么想法?
It seems weird to me that the function from a class definition would utterly disappear because you decorated it. In my mind that is even more reason to have a __decorated__
attribute applied for every decorator. However, it's more likely that my intuition is faulty and that the current behavior is what most people would expect. Any thoughts?
P.S。这是早期,更普遍的一个扩展<一个href=\"http://stackoverflow.com/questions/3375573/finding-a-functions-parameters-in-python\">question我有。我也去了解更多信息与一个<一个第一部分href=\"http://stackoverflow.com/questions/3505029/extending-the-$c$c-of-python-adding-language-features\">separate帖子。
p.s. this is an extension of an earlier, more general question I had. I also went for more info on the first part with a separate post.
推荐答案
好了,独立了,这是否是一个好主意任何讨论的,我会去的选项#3,因为它是最一致的:它总是表明,功能已经由属性的presence装饰,访问属性的值总是返回功能,所以您不必对它进行测试对无
Well, independent of any discussion over whether this is a good idea, I'd go for option #3 because it's the most consistent: it always shows that the function has been decorated by the presence of the attribute, and accessing the value of the attribute always returns a function, so you don't have to test it against None
.
您也应该考虑这一点,虽然:你会建议做一下手工装饰?例如。
You should also consider this, though: what would you propose to do about manual decoration? e.g.
def test(first): pass
test = d(test)
就问题的第一部分,我没有看过Python的跨preTER源$ C $ C非常多,所以我就没法给你指出什么特别有用。
Regarding the first part of the question, I haven't looked at the Python interpreter source code very much so I wouldn't be able to point you to anything particularly useful.
这篇关于__decorated__为Python装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!