Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand the python function description?

Tags:

python

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?

like image 610
misteryes Avatar asked Feb 17 '23 01:02

misteryes


2 Answers

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.

like image 97
Andreas Fester Avatar answered Feb 18 '23 14:02

Andreas Fester


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.

like image 42
Preet Kukreti Avatar answered Feb 18 '23 14:02

Preet Kukreti