简体   繁体   中英

Optional type hint in Google Style Guide

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM