Skip to content Skip to sidebar Skip to footer

Callback Function Not Callable

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 w

Solution 1:

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

Post a Comment for "Callback Function Not Callable"