Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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])
like image 387
orokusaki Avatar asked Jan 22 '10 21:01

orokusaki


People also ask

What do square brackets mean in a IF function?

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

What do square brackets mean in Python?

The indexing operator (Python uses square brackets to enclose the index) selects a single character from a string. The characters are accessed by their position or index value. For example, in the string shown below, the 14 characters are indexed left to right from postion 0 to position 13.


1 Answers

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])
like image 69
Mark Byers Avatar answered Nov 03 '22 11:11

Mark Byers