python 方括号“[]”在函数/类文档中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1718903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:56:09  来源:igfitidea点击:

What do square brackets, "[]", mean in function/class documentation?

python

提问by behindalens

I am having trouble figuring out the arguments to csv.dictreader and realized I have no clue what the square brackets signify.

我无法弄清楚 csv.dictreader 的参数,并意识到我不知道方括号表示什么。

From the docmentation:

从文档中:

class csv.DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]])

I'd appreciate a summary of the arguments to the class instantiation.

我很欣赏类实例化的参数摘要。

Thanks

谢谢

采纳答案by Stephan202

The square brackets indicate that these arguments are optional. You can leave them out.

方括号表示这些参数是可选的。你可以把它们排除在外。

So, in this case you are only requiredto pass the csvfileargument to csv.DictReader. If you would pass a second parameter, it would be interpreted as the fieldnamesarguments. The third would be restkey, etc.

因此,在这种情况下,你只需要传递的csvfile参数csv.DictReader。如果您要传递第二个参数,它将被解释为fieldnames参数。第三个是restkey,等等。

If you only want to specify e.g. cvsfileand dialect, then you'll have to name the keyword argument explicitly, like so:

如果您只想指定 eg cvsfileand dialect,那么您必须明确命名关键字参数,如下所示:

csv.DictReader(file('test.csv'), dialect='excel_tab')

For more on keyword arguments, see section 4.7.2of the tutorial at python.org.

有关关键字参数的更多信息,请参阅python.org 上教程的第 4.7.2 节

回答by Jeremy Wall

Usually in api documentation square brackets mean optional. I would think they mean the same here.

通常在 api 文档中方括号表示可选。我认为他们在这里的意思是一样的。

回答by Bandi-T

This is actually a subset of the widely used notation to unambiguously describe language syntax called Backus-Naur Form(see Wikipedia article for details).

这实际上是广泛使用的符号的一个子集,用于明确描述称为Backus-Naur 形式的语言语法(有关详细信息,请参阅维基百科文章)。

回答by pavium

To reiterate what the others have said, the arguments are optional.

重申其他人所说的话,这些论点是可选的。

If you leave out optional parts, the remaining fieldnames=, restval=, restkey=or dialect=keywords tell the function which parts are missing.

如果你离开了选装件,剩下的fieldnames=restval=restkey=dialect=关键字,告诉哪些部分缺失的功能。

The syntax doesn't suggest it, but I wouldn't be surprised if the keywords allow the arguments to be specificied in any order, except that the last two arguments must be either both specified, or both omitted.

语法不建议这样做,但如果关键字允许以任何顺序指定参数,我不会感到惊讶,除了最后两个参数必须要么都指定,要么都省略。