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

后端 未结 5 1615
灰色年华
灰色年华 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:48

    Because otherwise the correct syntax would be to include the comma even if you are ignoring the arguments. The parts inside the square brackets are optional, so by moving the commas out of the square brackets, they are no longer optional. For example, to call the function below with only a string:

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

    I would have to do:

    RegexObject.match("foobar",,)
    

    But, that isn't very elegant.

提交回复
热议问题