Any possible type alias that can also set a default value for a function arg?
Matthew Carruth
carruthm at gmail.com
Wed Oct 18 18:12:38 EDT 2023
We have the `Optional[T]` type as a short-hand for Union[T | None] and telling us that said argument may not be present.
However, I find that a majority of the time, we also want to set a default value of None on the argument so that it can be evaluated without doing a getattr() check first.
iow, a lot of `foo: Optional[str] = None` in method signatures.
I'd love to see a companion to the Optional type, I'll call it Default, so that it can take a default value as a second arg, with a default of that being None.
For example:
foo: Default[str] would be equivalent to foo: Optional[str] = None
foo: Default[str, "bar"] would be equivalent to foo: Optional[str] = "bar"
or something like that. Basically, any way to avoid writing `= None` over and over again.
More information about the Python-list
mailing list