[Solved] How to make function arguments optional? [duplicate]


Maybe this will help. In your case,

def action(file1, file2, behavior=defaultBehavior):
    return behave(file1, file2)

is okay. But of course, if an argument is passed into the “behave” parameter, you should take care of it in your function. For instance, if you called

action("somefile", "anotherfile", customBehavior)

then you’ll want something like the following to deal with it:

def action(file1, file2, behavior=defaultBehavior):

    if behave != defaultBehavior: # this means that behavior was set to something
        behave(file1, file2, behavior)
    else: # in this case, behave == defaultBehavior
        behave(file1, file2)

Something similar can be constructed for behave().

solved How to make function arguments optional? [duplicate]