I have been going through the opencv wiki when I came across the resize
function defined as:
dst = resize( src, dsize[, dst[, fx[, fy[, interpolation]]]] )
I am unable to understand what the [
after the arguments signify.
I was assuming that it means an optional argument, but I realized that cannot be the case as dsize
is a required argument.
Any clarification would help a lot.
This is just following typical Python documentation notation, as covered by the documentation.
The relevant part is quoted below for reference (emphasis mine)
The descriptions of lexical analysis and syntax use a modified Backus–Naur form (BNF) grammar 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).
Note that when talking about the syntax, the comma is not special in any way and should be treated as a literal.
Let's first consider an example from the itertools documentation page to see why the commas are enclosed in the square brackets in this particular way. In the table, the parameters of itertools.islice()
is specified as seq, [start,] stop [, step]
. Four possible function prototypes can be derived from it depending on the number of occurences of each group:
0 and 0 ocurrences:
islice(seq, stop)
0 and 1 occurences:
islice(seq, stop, step)
1 and 0 occurences:
islice(seq, start, stop)
1 and 1 occurences:
islice(seq, start, stop, step)
You can see that the comma has to be organized in this way with respect to the square brackets in order to derive all the four syntaxes correctly. If for example the grammar were written as seq, [start,] stop, [step]
, with the comma outside the second bracket, then for the 0 and 0 occurences case we would obtain islice(seq, stop,)
, resulting in a redundant comma.
The syntax in the question can be interpreted in exactly the same way. The key point here is at the formal grammar level, the comma is just a literal, you should not consider dst[
as a whole, which is where your confusion comes from.
For example, if we choose one occurence for the outermost bracket and zero for the others, we derive one possible syntax.
dst = resize( src, dsize[, dst[, fx[, fy[, interpolation]]]] )
1 0 0 0 0001
-> dst = resize( src, dsize, dst )
Another choice leads to another valid syntax.
dst = resize( src, dsize[, dst[, fx[, fy[, interpolation]]]] )
1 1 0 0 0011
-> dst = resize( src, dsize, dst, fx )
and so on. Now it should be clear why the comma need to be positioned in the way shown.
As already commented by others, Backus-Naur form is very common and not limited to Python. The details of modifications that Python made to the original BNF grammar can be found in the link above.