4

I'm a beginner at Python.

I don't understand the Python function description. For example, the function getopt

getopt.getopt(args, options[, long_options])

what does options[, long_options] mean?

4 Answers 4

5

It means that the part in square brackets is optional.

From http://docs.python.org/2/library/getopt.html:

long_options, if specified, must be a list of strings ...

In case you add this optional parameter, you also need to add the comma - if you do not add it, you also must not add the comma.

This notation is, BTW, a usual convention when specifying parameters, e.g. also for command line parameters which can be passed to a unix shell command.

Sign up to request clarification or add additional context in comments.

3 Comments

why there is a , and a space ` ` before long_options
Cause both are optional, the , are only needed if long_options are given.
and it's just like any other argument. You always put a comma and you [i]should[/i] put a space (due to convention), but I suppose you might say they are pointless.
3

You would be well served to learn about EBNF syntax, which is a way to specify syntax for various languages or commands in a formal way. While many tools' syntax documentations dont use strict EBNF, they often borrow its symbols. E.g. square brackets mean an optional component. The comma formally means concatenation, and is often used to imply optional multiple repetition of symbols in the context of square brackets.

Usage                Notation
definition           =
concatenation        ,
termination          ;
alternation          |
option               [ ... ]
repetition           { ... }
grouping             ( ... )
terminal string      " ... "
terminal string      ' ... '
comment              (* ... *)
special sequence     ? ... ?
exception            -

Some tools/documentation will also borrow from BNF syntax which uses a lot of angle brackets < ... > to specify symbols in terms of expressions.

Comments

0

For command line options the regular options are things like -h while the long option is --help. They mean the same thing and have the same effect but the short version is preceded by a single hyphen rather than two.

As has been noted in the other answers, in this case the options are required but the long_options are not which is why they are in square brackets.

Comments

0

It is a standard way to mark optional parameters to the function. So, in this case long_options is optional, but args and options are not. Comma after options is optional as well, but required if you specify long_options

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.