滥用功能的对象性质? [英] Abuse of the object-nature of functions?
问题描述
大家好,
在我编写的用于测试网站的框架中,我使用类似
的内容来添加各种功能要点:
#-----------------------------------
def do_work(可调用,数据):
断言=错误
尝试:
断言= callable.is_assertion
除外:
通过
out =可赎回(数据)
如果断言:
print" Test" %(失败,已成功)[退出]
退回
def get_assn(fn):
def包装器(* args,** kw):
返回fn(* args,** kw)
out =包装器
out.is_assertion = True
退货
def功能(数据):
返回True
x =功能
y = get_assn(功能)
do_work(x,数据)
do_work (y,数据)
#-------------------------------- ---
我的想法是我可以将某些函数标记为断言,并使用
将相同的函数应用于数据并可选
打印一些信息。这样我就不用担心
可调用是自定义对象还是简单函数。
问题是,这是否合理做?它有效,但它是否被认为是为函数添加属性的不良做法?还有
危险吗?
" Ant" < an **** @ gmail.comwrote:
问题是,这是否合理?
绝对是
。测试框架DSL对于
函数属性来说,这是一个非常有效的用例。
它可以工作,但是将属性添加到函数中是否被认为是不好的做法?
nope(至少不是小剂量;-)
还有危险吗?
nope。
< / F>
>
Sybren Stuvel写道:
Ant启发我们:
>尝试:
断言= callable.is_assertion
除了:
传递
尝试养成习惯,只捕捉你的例外情况知道会抛出
。抓住一切通常是个坏主意。在这种情况下,
我的赌注是捕捉AttributeError就足够了。
和
断言= hasattr(可调用,is_assertion)
甚至更好(并且可以在通话后作为if语句的一部分完成,在单独的尝试中除了
而不是在通话之前)
>
< / F>
Fredrik Lundh写道:
Sybren Stuvel写道:
> Ant启发我们:
>>尝试:
断言= callable.is_assertion
除了:
传递
尝试养成一个习惯,只捕捉你知道的例外
和
断言= hasattr(可调用,is_assertion)
甚至更好(并且可以在通话后作为if语句的一部分完成,
而不是单独的尝试/除了通话之前)
您通常希望通过将其设置为
False而不是删除它来关闭标记 - 这也是OP的代码的工作原理。所以我
更喜欢
断言= getattr(可调用,is_assertion,错误)
如果你放弃了明确的尝试......除了。
彼得
Hi all,
In a framework I''ve written to test out website, I use something like
the following to add functionality at various points:
#-----------------------------------
def do_work(callable, data):
assertion = False
try:
assertion = callable.is_assertion
except:
pass
out = callable(data)
if assertion:
print "Test " % ("Failed", "Suceeded")[out]
return out
def get_assn(fn):
def wrapper(*args, **kw):
return fn(*args, **kw)
out = wrapper
out.is_assertion = True
return out
def funct(data):
return True
x = funct
y = get_assn(funct)
do_work(x, data)
do_work(y, data)
#-----------------------------------
The idea is that I can mark some functions as being assertions, and use
the same function for applying the callable to the data and optionally
printing some information. This way I needn''t worry whether the
callable is a custom object or a simple function.
The question is, is this a reasonable thing to do? It works, but is it
considered bad practice to add attributes to functions? And are there
any dangers?
"Ant" <an****@gmail.comwrote:
The question is, is this a reasonable thing to do?absolutely. a test framework "DSL" is a perfectly valid use case for
function attributes.
It works, but is it considered bad practice to add attributes to functions?nope (at least not in small doses ;-)
And are there any dangers?nope.
</F>
Sybren Stuvel wrote:
Ant enlightened us with:> try:
assertion = callable.is_assertion
except:
pass
Try to make a habit out of catching only the exceptions you know will
be thrown. Catching everything generally is a bad idea. In this case,
my bet is that catching AttributeError is enough.and
assertion = hasattr(callable, "is_assertion")
is even nicer (and can be done as part of the if-statement after the call, in-
stead of in a separate try/except before the call)
</F>
Fredrik Lundh wrote:
Sybren Stuvel wrote:
>Ant enlightened us with:>> try:
assertion = callable.is_assertion
except:
pass
Try to make a habit out of catching only the exceptions you know will
be thrown. Catching everything generally is a bad idea. In this case,
my bet is that catching AttributeError is enough.
and
assertion = hasattr(callable, "is_assertion")
is even nicer (and can be done as part of the if-statement after the call,
in- stead of in a separate try/except before the call)You would normally expect that you can turn off a flag by setting it to
False instead of deleting it -- which is also how the OP''s code works. So I
would prefer
assertion = getattr(callable, "is_assertion", False)
if you forgo the explicit try...except.
Peter
这篇关于滥用功能的对象性质?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!