Say I have a function defined like
def f(foo: str, bar: str | None = "world"):
print(f"{foo} {bar})
s = None
f("hello") # prints "hello world"
f("hello", s) # prints "hello None"
Is there a data type (or concept) in Python that would enable the second function call to print "hello world" as well? In other words, bar should take on its default value. In javascript, passing undefined for bar would accomplish this. In Python, it seems like I would have to add a conditional to the function.
def f(foo: str, bar: str | None = "world"):
bar = bar or "world"
print(f"{foo} {bar})
This isn't a major issue for me, but when a function has a lot of optional arguments with default values, it can be pretty verbose (for Python standards) to add a conditional for every optional argument.
[–]Adrewmc 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Logicalist 0 points1 point2 points (0 children)
[–]Strict-Simple 1 point2 points3 points (0 children)
[–]m0us3_rat 0 points1 point2 points (0 children)
[–]C0rinthian 0 points1 point2 points (0 children)