I’m having a problem by trying to assign a lambda as a callback to optparse to handle the case where multiple -v’s are passed to the app, which should increment the global variable DEBUG each time a -v is seen.
# globals
DEBUG = 0
(...)
def main():
[...]
parser.add_option("-v", action="callback",
callback=lambda a,b,c,d: DEBUG += 1
help="Verbose (-vvv for added verboseness)")
When this gets executed, the result is:
callback=lambda a,b,c,d: DEBUG += 1
^
SyntaxError: invalid syntax
Any idea or alternative (clean) suggestion?
A lambda has to be an expression, while assignment like that is a statement. You will probably have to do it like this: