How does Python define a function with optional parameters through square brackets? … here is a solution to the problem.
How does Python define a function with optional parameters through square brackets?
I
often find functions defined as open(name[, mode[, buffering]]),
which I know means optional parameters.
The Python documentation says it’s a module-level function. When I try to define a function in this style, it always fails.
For example
def f([a[,b]]): print('123')
Does not work.
Can someone tell me what module level means and how to define a function with this style?
Solution
Is this what you are looking for?
>>> def abc(a=None,b=None):
... if a is not None: print a
... if b is not None: print b
...
>>> abc("a")
a
>>> abc("a","b")
a
b
>>> abc()
>>>