How to understand python function description?

I am starting in Python.

I do not understand the description of the Python function. For example, the getopt function

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

what does options[, long_options] ?

+4
source share
4 answers

This means that the part in square brackets is optional .

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

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

If you add this optional parameter, you also need to add a comma - if you do not add it, you also should not add a comma.

This designation, BTW, the usual convention when specifying parameters, for example. also for command line options that can be passed to the unix shell command.

+5
source

You will be good to listen to learn about EBNF syntax, which is a way to specify syntax for various languages ​​or commands in a formal way. Although the syntax documents of many tools do not use strict EBNF, they often borrow their characters. For instance. square brackets indicate an optional component. The comma formally means concatenation and is often used to indicate optional multiple repetition of characters 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 be used in the BNF syntax, which uses many angle brackets < ... > to specify characters in terms of expressions.

+3
source

For command line options, regular options such as -h , while the long option --help . They mean the same thing and have the same effect, but the short version is preceded by one hyphen, not two.

As noted in other answers, options are required in this case, but long_options are not why they are enclosed in square brackets.

0
source

This is the standard way to mark optional parameters for a function. So in this case long_options is optional, but args and options are not. The options comma is also optional, but required if you specify long_options

0
source

Source: https://habr.com/ru/post/1489286/


All Articles