为什么Python函数文档在可选参数的括号后面包含逗号? [英] Why do Python function docs include the comma after the bracket for optional args?

查看:702
本文介绍了为什么Python函数文档在可选参数的括号后面包含逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python文档中的函数签名的格式有点混乱。将逗号放在开放式括号之后,而不是之前有什么意义?嵌套括号的意义是什么?



它们是如何:

  RegexObject.match(string [,pos [,endpos]] 

以下内容:

  RegexObject.match(string,[pos],[endpos])
RegexObject.match [,pos] [,endpos])


解决方案

括号表示内容是可选的,但方括号之外的所有内容都是强制性的。



使用您的符号:

  RegexObject.match(string,[pos],[endpos])

我希望写:

  r.match(foo,,)

嵌套是必需的,因为如果提供第三个参数,那么您也必须提供第二个参数,即使它是可选参数。以下非嵌套替代方案将是不明确的:

  RegexObject.match(string [,pos] [,endpos])


The format of the function signatures in the Python docs is a bit confusing. What is the significance in putting the comma after the open bracket, rather than before? What is the significance of nesting the brackets?

How they are:

RegexObject.match(string[, pos[, endpos]])

I would expect one of the following:

RegexObject.match(string, [pos], [endpos])
RegexObject.match(string[, pos][, endpos])

解决方案

The square bracket means that the contents are optional, but everything outside of square brackets is compulsory.

With your notation:

RegexObject.match(string, [pos], [endpos])

I would expect to have to write:

r.match("foo",,)

The nesting is required because if you supply the third parameter then you must also supply the second parameter even though it is an optional parameter. The following non-nested alternative would be ambiguous:

RegexObject.match(string[, pos][, endpos])

这篇关于为什么Python函数文档在可选参数的括号后面包含逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆