Consider a function in JavaScript:
If val
is not defined in the first call, it becomes 0
function someRecursiveFn (item, val) {
val = val || 0;
...
}
How do I assign the same way in Python?
def someRecursiveFn(item, val):
val = ??
...
You could use a keyword argument instead of a plain argument to your function:
def someRecursiveFn(item, val=None):
val = val or 0
so val
will default to None
if it's not passed to the function call.
the val = val or 0
will ensure that val=None
or val=''
are converted to 0. Can be omitted if you only care about val
being defined in the first place.
val = val if val else 0
#if val is not None, it will assign itself, if it is None it will set val=0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With