I have a doubt regarding Optional
type hint in the Google Python Style Guide. In section 3.19.5, the following is shown as correct usage:
def func(a: Optional[Text], b: Optional[Text] = None) -> Text:
I don't understand a: Optional[Text]
. Why is it not a: Optional[Text] = None
?
Just because it's Optional
doesn't mean that it needs a default argument, or that the argument is "optional".
Optional[Text]
means "it can be a Text
object, or it can be None
". The None
value need not be a specified default though; it can be user supplied. You may, for whatever reason, want the user to pass that argument, even if it's just None
.
Part of the confusion might be the use of the term "optional" here. "Optional" in this context doesn't mean that the argument is optional. It means that it's an option type.