Consider the following function, which does not work in Python, but I will use to explain what I need to do.
def exampleFunction(a, b, c = a):
...function body...
That is I want to assign to variable c the same value that variable a would take, unless an alternative value is specified. The above code does not work in python. Is there a way to do this?
The default value for the keyword argument can’t be a variable (if it is, it’s converted to a fixed value when the function is defined.) Commonly used to pass arguments to a main function:
If
Nonecould be a valid value, the solution is to either use*args/**kwargsmagic as in carl’s answer, or use a sentinel object. Libraries that do this include attrs and Marshmallow, and in my opinion it’s much cleaner and likely faster.The only way for
c is missingto be true is forcto be exactly that dummy object you created there.