可赎回的断言? [英] Callable assertion?
问题描述
我有一个函数可以通过回调函数作为
参数。我想检查以确保传入的内容确实可以调用
。是:
断言''__call__''在dir(param)
正确的做法,还是有更清洁的东西?对于param的所有可调用值,它是否会工作
,无论它是静态函数,
类方法,内置等等?
我正在运行2.3.1。对于我来说,向后兼容性并不是特别的问题
,但我想知道这个
的工作时间有多久会很好。
I''ve got a function which gets passed a call-back function as a
parameter. I want to check to make sure the thing passed in is indeed
callable. Is:
assert ''__call__'' in dir (param)
the right thing to do, or is there something cleaner? Will that work
for all callable values of param, regardless if it''s a static function,
class method, built-in, etc?
I''m running 2.3.1. Backward compatability isn''t particularly an issue
for me, but I guess it would be nice to know how far back in time this
works.
推荐答案
On Sun,05 Oct 2003 08:55:45 -0400,
Roy Smith< ro*@panix.com>写道:
On Sun, 05 Oct 2003 08:55:45 -0400,
Roy Smith <ro*@panix.com> wrote:
正确的事情,还是有更清洁的东西?这对于param的所有可调用值是否有效,无论它是静态函数,类方法,内置等等?
the right thing to do, or is there something cleaner? Will that work
for all callable values of param, regardless if it''s a static function,
class method, built-in, etc?
有一个可调用的(param)内置函数,可以追溯到Python 1.2。
--amk
There''s a callable(param) built-in function, dating back to Python 1.2.
--amk
文章< wM ******************** @ speakeasy.net>,
" AM了Kuchling" < am*@amk.ca>写道:
In article <wM********************@speakeasy.net>,
"A.M. Kuchling" <am*@amk.ca> wrote:
On Sun,05 Oct 2003 08:55:45 -0400,
Roy Smith< ro*@panix.com>写道:
On Sun, 05 Oct 2003 08:55:45 -0400,
Roy Smith <ro*@panix.com> wrote:
正确的事情,还是有更清洁的东西?这对于param的所有可调用值是否有效,无论它是静态函数,类方法,内置等等?
the right thing to do, or is there something cleaner? Will that work
for all callable values of param, regardless if it''s a static function,
class method, built-in, etc?
'可调用(param)内置函数,可追溯到Python 1.2。
- amk
There''s a callable(param) built-in function, dating back to Python 1.2.
--amk
啊。这正是我想要的!文档说:
"如果对象参数显示为可调用,则返回true,否则返回false。如果
返回true,则调用仍然可能失败,但如果它是
false,则调用object将永远不会成功。
看似可调用的是什么意思?在什么情况下
callable(foo)返回True,但foo()会失败?
Ah. That''s exactly what I was looking for! The docs say:
"Return true if the object argument appears callable, false if not. If
this returns true, it is still possible that a call fails, but if it is
false, calling object will never succeed."
What does "appears callable" mean? Under what circumstances would
callable(foo) return True, yet foo() would fail?
" ;罗伊史密斯 < ro*@panix.com>在消息中写道
news:ro *********************** @ reader2.panix.com ..。
"Roy Smith" <ro*@panix.com> wrote in message
news:ro***********************@reader2.panix.com.. .
我有一个函数可以将回调函数作为
参数传递。我想检查以确保传入的内容是
确实可以调用。
I''ve got a function which gets passed a call-back function as a
parameter. I want to check to make sure the thing passed in is indeed callable.
酸测试是尝试调用它:
try:param()
除了TypeError,msg:<做什么>
你可以查看msg.endswith(' 'not callable'')如果你想要,但是我不知道错误信息的稳定性。
Terry J. Reedy >
The acid test is to try calling it:
try: param()
except TypeError, msg: <do whatever>
You can check that msg.endswith(''not callable'') if you want but I
don''t know the stability of the error message.
Terry J. Reedy
这篇关于可赎回的断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!