Why do Python function docs include the comma after the bracket for optional args?

后端 未结 5 1597
灰色年华
灰色年华 2020-12-03 04:08

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

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 04:55

    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])
    

提交回复
热议问题