Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Python function arg notation like this: str.find(sub[, start[, end]])

Tags:

python

Can someone describe what's happening syntactically in functions like functions like str.find(sub[, start[, end]])? I don't understand what the function arguments are doing or supposed to be.

For one thing, what is the [, x] construct?

like image 216
Yarin Avatar asked Nov 19 '11 15:11

Yarin


2 Answers

This square bracket notation is used to denote optional arguments. You can omit them if you wish and thus invoke default behaviour. The optional arguments will always be described in the documentation.

Note that the square brackets used in the documentation are not part of Python itself. You can never write that in a Python program. It is purely a notational convention used in documentation.

like image 72
David Heffernan Avatar answered Oct 22 '22 08:10

David Heffernan


I leave this for future reference:

A similar question in programmer.se

A link to Python documentation. Basically:

Each rule begins with a name (which is the name defined by the rule) and ::=. A vertical bar (|) is used to separate alternatives; it is the least binding operator in this notation. A star (*) means zero or more repetitions of the preceding item; likewise, a plus (+) means one or more repetitions, and a phrase enclosed in square brackets ([ ]) means zero or one occurrences (in other words, the enclosed phrase is optional). The * and + operators bind as tightly as possible; parentheses are used for grouping. Literal strings are enclosed in quotes. White space is only meaningful to separate tokens. Rules are normally contained on a single line; rules with many alternatives may be formatted alternatively with each line after the first beginning with a vertical bar.

like image 26
Robert Smith Avatar answered Oct 22 '22 07:10

Robert Smith