装饰器,身份功能和执行...... [英] Decorators, Identity functions and execution...
问题描述
如果我定义一个装饰器,如:
def t(x):
def I(x):return x
返回我
并使用它如:
@t(X)
def foo(a ):
#foo的定义...
通过
或者这个:
@t(X)
@(Y)
def bar(a):
#栏的定义.. 。
在执行
''foo''或''bar''时会遇到很多惩罚吗?如果是这样,有没有办法定义
t,这样Python知道它是身份函数
和短路评估?
提前致谢。
If I define a decorator like:
def t(x) :
def I(x) : return x
return I
and use it like:
@t(X)
def foo(a) :
# definition of foo...
pass
or maybe this:
@t(X)
@(Y)
def bar(a) :
# The definition of bar...
Will in encounter much of a penalty in executing
''foo'' or ''bar''? If so, is there a way to define
t such that Python knows it is the identity function
and short-circuit evaluation?
Thanks in advance.
推荐答案
Chance Ginger"写道:
Chance Ginger" wrote:
如果我定义一个装饰器如:
def t(x):
def I(x):return x
返回我
....你得到一个语法错误。
并使用它像:
@t(X )
def foo(a):
#foo的定义......
传递
这也是一个语法错误。 />
或者这个:
@t(X)
@(Y)
def吧(一):
#bar的定义...
并且甚至无法修复。
将在执行中遇到大量惩罚
''foo''还是''酒吧''?
因为foo被包裹了,调用foo会调用你的I函数,在
中调用原来的foo。
如果那么,有没有办法定义t使得Python知道它是身份功能和短路评估?
If I define a decorator like:
def t(x) :
def I(x) : return x
return I
.... you get a syntax error.
and use it like:
@t(X)
def foo(a) :
# definition of foo...
pass
that''s also a syntax error.
or maybe this:
@t(X)
@(Y)
def bar(a) :
# The definition of bar...
and that''s not even fixable.
Will in encounter much of a penalty in executing
''foo'' or ''bar''?
since foo is wrapped, calling foo will call your I function, which in
turn calls the original foo.
If so, is there a way to define t such that Python knows it is
the identity function and short-circuit evaluation?
如果你不是我想包装一些东西,不要包装它:
def t(x):
def I(x):
返回x
如果日期==星期五:
返回x#不要换行
返回我#wrap it
< / F>
if you don''t want to wrap something, don''t wrap it:
def t(x) :
def I(x) :
return x
if date == friday:
return x # don''t wrap it
return I # wrap it
</F>
On Sun,2006年4月9日09:51:18 +0200,Fredrik Lundh写道:
On Sun, 09 Apr 2006 09:51:18 +0200, Fredrik Lundh wrote:
Chance Ginger"写道:
Chance Ginger" wrote:
如果我定义一个装饰器如:
def t(x):
def I(x):return x
返回我
...你得到一个语法错误。
If I define a decorator like:
def t(x) :
def I(x) : return x
return I
... you get a syntax error.
这不是语法错误...我在发布之前尝试过。事实上
def t(x):
def I(x):返回x
返回我
是正确的。
It isn''t a syntax error...I tried it before I posted. In fact
def t(x) :
def I(x) : return x
return I
is correct.
并使用它:
@t(X)
def foo(a):
#foo的定义......
传递
and use it like:
@t(X)
def foo(a) :
# definition of foo...
pass
这也是一个语法错误。
that''s also a syntax error.
假如你传入一个有效的''X'',这不是一个错误。
Once again this isn''t an error assuming you pass in a valid ''X''.
或者这个:
@t(X)
@(Y)
def吧(一):
#吧的定义......
or maybe this:
@t(X)
@(Y)
def bar(a) :
# The definition of bar...
并且这甚至无法修复。
and that''s not even fixable.
再次你错了。如果我说:
@t(1)
@t(2)
def bar(a):传递
这是完全有效的。
Again you are mistaken. If I say:
@t(1)
@t(2)
def bar(a) : pass
It is perfectly valid.
在执行中会遇到很多惩罚
''foo''或''bar''?
Will in encounter much of a penalty in executing
''foo'' or ''bar''?
因为foo被包装,调用foo将调用你的I函数,它在
中调用原来的foo。
since foo is wrapped, calling foo will call your I function, which in
turn calls the original foo.
如果是这样,有没有办法定义t使Python知道它是身份功能和短路评估?
If so, is there a way to define t such that Python knows it is
the identity function and short-circuit evaluation?
如果你不我想包装一些东西,不要包装它:
def t(x):
def我(x):
返回x
如果date == friday:
返回x#不要换行
返回我#wrap it
< / F>
if you don''t want to wrap something, don''t wrap it:
def t(x) :
def I(x) :
return x
if date == friday:
return x # don''t wrap it
return I # wrap it
</F>
装饰器是一种添加语法的方法。糖到Python,
以对工具有用的方式扩展它。什么
我尝试做的是减少使用某些形式的装饰器时执行Python代码所花费的时间对
的影响。
Decorators are a way to add "syntactic" sugar to Python,
extending it in ways that make it useful for tools. What
I am trying to do is lessen the impact on the time used
in executing Python code when I use some forms of decorators.
Chance Ginger< cg ******** @ hotmail.com>写道:
Chance Ginger <cg********@hotmail.com> writes:
On Sun,2006年4月9日09:51:18 +0200,Fredrik Lundh写道:
On Sun, 09 Apr 2006 09:51:18 +0200, Fredrik Lundh wrote:
Chance Ginger"写道:
Chance Ginger" wrote:
如果我定义一个装饰器如:
def t(x):
def I(x):return x
返回我
...你得到一个语法错误。
If I define a decorator like:
def t(x) :
def I(x) : return x
return I
... you get a syntax error.
这不是语法错误......我在发布之前尝试过。事实上
def t(x):
def我(x):返回x
返回我
是正确的。
It isn''t a syntax error...I tried it before I posted. In fact
def t(x) :
def I(x) : return x
return I
is correct.
的确如此。这是对的。 Fredrick的评论与你的代码中缺少
缩进有关。
Indeed. This is correct. Fredrick''s comment was related to the lack of
indentation in your code.
并使用它像: br />
@t(X)
def foo(a):
#foo的定义......
传递
and use it like:
@t(X)
def foo(a) :
# definition of foo...
pass
这也是一个语法错误。
that''s also a syntax error.
如果传入一个有效的''X'',这不是一个错误。
Once again this isn''t an error assuming you pass in a valid ''X''.
如果你的缩进如上所述,那么'X'是什么并不重要。
-
Jorge Godoy< go *** @ ieee.org>
" Quidquid latine dictum sit,altum sonatur。"
- Qualquer coisa dita em latim soa profundo。
- 用拉丁语说的任何东西听起来很聪明。
If your indentation is broken as above it doesn''t matter what ''X'' is.
--
Jorge Godoy <go***@ieee.org>
"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
这篇关于装饰器,身份功能和执行......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!