Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do square brackets, "[]", mean in function/class documentation?

Tags:

python

I am having trouble figuring out the arguments to csv.dictreader and realized I have no clue what the square brackets signify.

From the docmentation:

class csv.DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]]) 

I'd appreciate a summary of the arguments to the class instantiation.

Thanks

like image 957
behindalens Avatar asked Nov 11 '09 23:11

behindalens


People also ask

What does square bracket mean in documentation?

Square brackets indicate optional parts of a statement. They should not be entered. In many cases, items in the square brackets are optional because default values are provided. | A vertical bar indicates a choice between two or more items or values, usually within square brackets or curly braces.

What is the function of square brackets in Python?

The indexing operator (Python uses square brackets to enclose the index) selects a single character from a string. The characters are accessed by their position or index value.

What does square brackets mean in syntax?

mvBASIC Syntax NotationsAnything shown enclosed within square brackets is optional unless indicated otherwise. The square brackets themselves are not typed unless they are shown in bold. | A vertical bar that separates two or more elements indicates that any one of the elements can be typed.

Which brackets are used to give function arguments?

The parentheses are used for passing arguments into a function.


1 Answers

The square brackets indicate that these arguments are optional. You can leave them out.

So, in this case you are only required to pass the csvfile argument to csv.DictReader. If you would pass a second parameter, it would be interpreted as the fieldnames arguments. The third would be restkey, etc.

If you only want to specify e.g. cvsfile and dialect, then you'll have to name the keyword argument explicitly, like so:

csv.DictReader(file('test.csv'), dialect='excel_tab') 

For more on keyword arguments, see section 4.7.2 of the tutorial at python.org.

like image 125
Stephan202 Avatar answered Sep 16 '22 15:09

Stephan202