Decorater里面有一个功能?有办法吗? [英] Decorater inside a function? Is there a way?

查看:39
本文介绍了Decorater里面有一个功能?有办法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在试图通过添加一个

装饰器来弄清楚如何测试函数参数。


@装饰

def func(x):

#做点什么

返回x


这允许我用自己的包裹和替换参数,但不是

得到原始函数收到的参数。


要做到这一点,我需要把函数内部的装饰器。


def func(x):

@decorate

#doc something

返回x


然后我可以使用@decorators来检查

条件,范围和/或类型的函数输入。


有没有相同的方法呢?


我也可以使用@decorate断言吗?

Ron


I''m trying to figure out how to test function arguments by adding a
decorator.

@decorate
def func( x):
# do something
return x

This allows me to wrap and replace the arguments with my own, but not
get the arguments that the original function received.

To do that I would need to put the decorator inside the function.

def func( x):
@decorate
# doc something
return x

Then I could use @decorators to check the function input for
condition, ranges, and or types.

Is there a equivalent way to do that?

Also can I use @decorate with assert?
Ron

推荐答案

2005年4月1日星期五18:30:56 +0000,Ron_Adam写道:
On Fri, 01 Apr 2005 18:30:56 +0000, Ron_Adam wrote:
我是试图弄清楚如何测试函数论证添加一个
装饰器。
I''m trying to figure out how to test function arguments by adding a
decorator.




然后你的其余信息将生动地展示为什么

装饰者为糟糕的测试技术。


这是愚人节吗?如果是这样的话,它就不是一个非常好的,因为它很符合我之前看过的那种问题。 我有

a锤子,我怎么用它来给我的轮胎充气呢?


假设你很认真,为什么不使用其中一个实际上为它设计的许多测试技术实际上是为b / b
如何完成各种任务?从你想要做的事情开始,然后

如何去做。



The rest of your message then goes on to vividly demonstrate why
decorators make for a poor test technique.

Is this an April Fools gag? If so, it''s not a very good one as it''s quite
in line with the sort of question I''ve seen many times before. "I have
a hammer, how do I use it to inflate my tire?"

Assuming you''re serious, why not use one of the many testing technologies
actually designed for it, and tap into the associated body of knowledge on
how to accomplish various tasks? Start with what you''re trying to do, then
work on how to do it.


2005年4月1日星期五13: 47:06 -0500,Jeremy Bowers< je ** @ jerf.org>

写道:
On Fri, 01 Apr 2005 13:47:06 -0500, Jeremy Bowers <je**@jerf.org>
wrote:
2005年4月1日星期五18:30: 56 +0000,Ron_Adam写道:
On Fri, 01 Apr 2005 18:30:56 +0000, Ron_Adam wrote:
我正在试图通过添加一个
装饰器来弄清楚如何测试函数参数。
然后你的其余信息继续生动地展示了为什么
装饰器会导致测试技术不佳。
I''m trying to figure out how to test function arguments by adding a
decorator.
The rest of your message then goes on to vividly demonstrate why
decorators make for a poor test technique.




所以这是不可能的。好的,谢谢。

这是愚人节吗?如果是这样的话,它不是一个非常好的,因为它非常符合我之前见过的那种问题。 我有一把锤子,怎么用它来给我的轮胎充气呢?


不是四月份的傻瓜,我只是新装饰者和谷歌带来

过去很多关于如何实施它们的讨论在

未来,但实际上他们的工作方式或如何使用它们并不多。

他们似乎没有在目前很好地记录,可能是因为

它们的语法和/或功能并没有完全决定。我已经能够从我找到的b $ b $例子中找出基本原理,但这并不意味着没有更多我还没有找到
的可能性。

假设你是认真的,为什么不使用为它设计的众多测试技术之一,并了解如何完成各种任务的相关知识体系?从你想要做的事情开始,然后
研究如何去做。



So it''s not possible to do. Ok, thanks.
Is this an April Fools gag? If so, it''s not a very good one as it''s quite
in line with the sort of question I''ve seen many times before. "I have
a hammer, how do I use it to inflate my tire?"
Not an April fools gag, I''m just new to decorators and google brings
up lots of discussions from the past on how they may be implemented in
the future, but not much in actually how they work or how to use them.
They don''t seem to be documented well at the present, possibly because
the syntax and or function of them isn''t completely decided on. I''ve
been able to figure out the basic principle from the examples I''ve
found, but that doesn''t mean there isn''t more possibilities I haven''t
found yet.
Assuming you''re serious, why not use one of the many testing technologies
actually designed for it, and tap into the associated body of knowledge on
how to accomplish various tasks? Start with what you''re trying to do, then
work on how to do it.




我正在努力理解这个用法''

装饰者的限制和可能性。


我刚刚想到包装一个函数的内容vs

包装它自己的功能,可能很有用。


Ron



I''m trying to understand the use''s, limits, and possibilities of
decorators.

It just occurred to me that wrapping the contents of a function vs
wrapping the function it''s self, could be useful.

Ron


4月01日星期五2005年19:56:55 +0000,Ron_Adam写道:
On Fri, 01 Apr 2005 19:56:55 +0000, Ron_Adam wrote:
2005年4月1日星期五13:47:06 -0500,Jeremy Bowers< je ** @ jerf.org>
写道:
On Fri, 01 Apr 2005 13:47:06 -0500, Jeremy Bowers <je**@jerf.org>
wrote:
这是愚人节吗?如果是这样的话,它不是一个非常好的,因为它非常符合我之前见过的那种问题。 我有一把锤子,我怎么用它来给我的轮胎充气呢?
不是四月的傻瓜堵嘴,我不是装修者的新手,谷歌带来了很多
过去关于如何在未来实施它们的讨论,但实际上它们的工作原理或使用方式并不多。
Is this an April Fools gag? If so, it''s not a very good one as it''s quite
in line with the sort of question I''ve seen many times before. "I have
a hammer, how do I use it to inflate my tire?"
Not an April fools gag, I''m just new to decorators and google brings
up lots of discussions from the past on how they may be implemented in
the future, but not much in actually how they work or how to use them.




OK ,只是检查:-)


装饰师原则上完全等同于


def功能():

传递

function = decorator(函数)


这是一种简化形式;装饰器本身可以是一个表达式,其中
返回一个可应用于函数的可调用函数和

的规则,按照您的预期应用多个顺序工作(流水线操作)之前的

会导致后来的那些,基于函数名称误导技巧制作出一个很棒的混淆Python条目或

两个,但是这个简化了

表单捕获了essense,这是我认为你正在寻找的。特别是

,它只是语法糖,而不是特殊功能。

我正在试图理解使用''
装饰器的限制和可能性。

我刚想到,将函数的内容包装起来包装它自己的函数可能很有用。



OK, just checking :-)

A decorator is completely equivalent in principle to

def function():
pass
function = decorator(function)

That''s a simplified form; decorators can themselves be an expression which
returns a callable that can be applied to a function and the rule for
applying several in sequence work as you''d expect (pipelining earlier
results into later ones, making for a great Obfuscated Python entry or
two based on the "function name misdirection" trick), but this simplified
form captures the essense, which is what I think you''re looking for. In
particular, it''s just "syntax sugar", not a "special feature".
I''m trying to understand the use''s, limits, and possibilities of
decorators.

It just occurred to me that wrapping the contents of a function vs
wrapping the function it''s self, could be useful.




装饰器,字面上只能包装函数。你可以写一个包装器

然后对参数做一些事情,人们有时会这样做,但是你不能直接包裹。参数。


注意,在向您展示装饰器如何工作之后,您可以手动进行操作。自己申请

装饰师:

Python 2.3.5(#1,2005年3月3日,17:32:12)

linux2上的[GCC 3.4.3(Gentoo Linux 3.4.3,ssp-3.4.3-0,pie-8.7.6.6)]



Decorators, literally, can only wrap functions. You can write a wrapper
then that does something to the arguments, which people sometimes do, but
you can''t directly "wrap" the arguments.

Note, having shown you how decorators work, you can "manually" apply the
decorator yourself:

Python 2.3.5 (#1, Mar 3 2005, 17:32:12)
[GCC 3.4.3 (Gentoo Linux 3.4.3, ssp-3.4.3-0, pie-8.7.6.6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import string
string._join = string.join
def joinWrap(* args ,** kwargs):
.... print args,kwargs

.... return" My Wrapper",string._join(* args,** kwargs)

.... string.join = joinWrap
string.join([" 1"," 2"," 3"],"")
我的包装(['''','''',''''',''|''){}

''1 | 2 | 3''
import string
string._join = string.join
def joinWrap(*args, **kwargs): .... print args, kwargs
.... return "My Wrapper", string._join(*args, **kwargs)
.... string.join = joinWrap
string.join(["1","2","3"], "|") My Wrapper ([''1'', ''2'', ''3''], ''|'') {}
''1|2|3''




所以,无论你想要做什么,仍然可以在没有

装饰器语法的情况下完成,并且*这个*并非闻所未闻,但如果你没有使用

,前几次正确管理

引用可能会很棘手。 (注意替换的函数(在这个例子中加入)可以去任何地方

包装器可以得到它,我只是将它粘在原始模块中,简单地用于

。)这不是我要做的第一件事,事实上在我所有的b $ b测试代码中我都不认为我做过这个,但它在工具箱中。


这样做而不是滥用装饰器语法;你可以写一个

装饰器,试图弄清楚它是否在测试

环境中运行并有条件地影响该功能,但这可能是一个

坏主意。


感觉就像我欠你一个回答 - 在四月愚蠢的指责之后,

Jeremy Bowers

:-)



So, whatever it is you are trying can do can still be done without the
decorator syntax, and *this* is not unheard of, though managing the
references correctly can be tricky the first few times if you''re not used
to it. (Note the replaced function (join in this example) can go anywhere
the wrapper can get at it, I just stick it back in the original module for
simplicity.) It''s not the first thing I reach for, in fact in all my
testing code I don''t think I ever do this, but it is in the toolbox.

Do this instead of abusing the decorator syntax; you could write a
decorator that tries to figure out if it''s being run in a testing
environment and conditionally affects the function, but that''s probably a
bad idea.

Feeling-like-I-owed-you-an-answer-after-the-april-fool-accusation-ly yrs,
Jeremy Bowers
:-)


这篇关于Decorater里面有一个功能?有办法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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