Callback function not callable

后端 未结 1 1684
天命终不由人
天命终不由人 2021-01-24 17:35

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

1条回答
  •  无人及你
    2021-01-24 18:05

    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.)

    0 讨论(0)
提交回复
热议问题