回调函数不可调用 [英] Callback function not callable

查看:31
本文介绍了回调函数不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 文档中读到可以从命令行调用函数,所以我使用 optparse 模块从函数返回一个巨大的文本,但我的代码没有工作!我认为我做对了一切.

def HelpDoc():返回 """ 一些巨大的文本 """parser = OptionParser(usage="%prog ConfigFile")parser.add_option("-g", "--guide", action = "callback", callback=HelpDoc(), help = "显示帮助文档")(options,args) = parser.parse_args()

追溯

 parser.add_option("-g", "--guide", action = "callback", callback=HelpDoc(), help = "显示帮助文档")文件/Python-2.7.2/lib/python2.7/optparse.py",第 1012 行,在 add_optionoption = self.option_class(*args, **kwargs)文件/Python-2.7.2/lib/python2.7/optparse.py",第 577 行,在 __init__ 中检查员(自己)_check_callback 中的文件/Python-2.7.2/lib/python2.7/optparse.py",第 712 行回调不可调用:%r"% self.callback, self)

解决方案

HelpDoc() 是字符串,不是回调函数,所以使用 callback=HelpDoc 代替,即:

parser.add_option("-g", "--guide", action = "callback", callback=HelpDoc, help = "显示帮助文档")

这里的区别可以通过以下方式看出:

<预><代码>>>>类型(帮助文档())字符串>>>类型(帮助文档)功能

所以,这就是为什么抱怨回调对象不可调用的原因.字符串显然不能作为函数调用.

但是,选项回调还有一些进一步的要求,因此使用上面的修复程序,您只会收到另一个错误(参数太多).有关更多信息和示例,请参阅:https://docs.python.org/2/library/optparse.html#optparse-option-callbacks

所以,它比那要复杂一些.至少函数签名(接受的参数)必须是正确的.

(正如 Shadow9043 在其评论中所说,optparse 已被弃用,请改用 argparse.)

I've read in python documentation that it is possible to call a function from command line, so I've used optparse module to return a huge text from a function but my code doesn't work! I think I've done everything right.

def HelpDoc():
     return """ SOME 
                   HUGE 
                      TEXT """

parser = OptionParser(usage="%prog ConfigFile")
parser.add_option("-g", "--guide", action = "callback", callback=HelpDoc(), help = "Show help documentation")

(options,args) = parser.parse_args()

Traceback

    parser.add_option("-g", "--guide", action = "callback", callback=HelpDoc(), help = "Show help documentation")
  File "/Python-2.7.2/lib/python2.7/optparse.py", line 1012, in add_option
    option = self.option_class(*args, **kwargs)
  File "/Python-2.7.2/lib/python2.7/optparse.py", line 577, in __init__
    checker(self)
  File "/Python-2.7.2/lib/python2.7/optparse.py", line 712, in _check_callback
    "callback not callable: %r" % self.callback, self)

解决方案

HelpDoc() is string, not a callback function, so use callback=HelpDoc instead, i.e.:

parser.add_option("-g", "--guide", action = "callback", callback=HelpDoc, help = "Show help documentation")

The difference here can be seen by:

>>> type(HelpDoc())
str

>>> type(HelpDoc)
function

So, that is why the complaint is that the callback object is not callable. String clearly cannot be called as a function.

However, there are certain further requirements for an option callback, so with the fix above you'll just receive another error (too many arguments). For more info and examples, see: https://docs.python.org/2/library/optparse.html#optparse-option-callbacks

So, it is a bit more complicated than that. At least the function signature (accepted parameters) has to be right.

(And as Shadow9043 says in its comment, optparse is deprecated, use argparse instead.)

这篇关于回调函数不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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