简体   繁体   English

Google Style Guide 中的可选类型提示

[英]Optional type hint in Google Style Guide

I have a doubt regarding Optional type hint in the Google Python Style Guide .我对Google Python Style Guide中的Optional类型提示有疑问。 In section 3.19.5, the following is shown as correct usage:在第 3.19.5 节中,正确用法如下所示:

def func(a: Optional[Text], b: Optional[Text] = None) -> Text:

I don't understand a: Optional[Text] .我不明白a: Optional[Text] Why is it not a: Optional[Text] = None ?为什么不是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的并不意味着它需要一个默认参数,或者该参数是“可选的”。

Optional[Text] means "it can be a Text object, or it can be None ". Optional[Text]表示“它可以是Text object,也可以是None ”。 The None value need not be a specified default though;不过, None值不必是指定的默认值; it can be user supplied.它可以由用户提供。 You may, for whatever reason, want the user to pass that argument, even if it's just None .无论出于何种原因,您都可能希望用户传递该参数,即使它只是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 .这意味着它是一个选项类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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