Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one read the function signatures from Python's official documentation

If one browses through the official Python docs, one can see function (or class) signatures of varying kinds.

For example

random.uniform(a, b)

is simple to understand: You pass it two objects a and b (that are floating point numbers and it returns a random number from the interval between them). Similarly simple to understand is the signature from

SSLSocket.getpeercert(binary_form=False)

where a default value for the argument is also specified, in case it's called without any argument.


But then there are also functions with really weird signatures like

min(iterable, *[, key, default])

or

readline.append_history_file(nelements[, filename])

or

csv.register_dialect(name[, dialect[, **fmtparams]])

What do these all mean? Is there some reference guide explaining how to read things like name[, dialect[, **fmtparams]]?

These example were just randomly taken from the official Python docs and don't cover all signature types I've come across. I need a general explanation how to read these signatures.

like image 889
l7ll7 Avatar asked Feb 06 '19 12:02

l7ll7


1 Answers

The asterisk in the example below means that key and default are keyword only arguments:

min(iterable, *[, key, default])

Parameter in square brackets are optional, so below filename is optional:

readline.append_history_file(nelements[, filename])

Argument with a single asterisk mean that the function can have any number of positional arguments, for instance:

a_function(a, b, *args)

Argument with 2 asterisks mean that the function can have any number of keyword arguments, for instance:

class dict(mapping, **kwarg)

Everything is explained in the Python documentation: Function definitions

like image 170
Laurent LAPORTE Avatar answered Oct 13 '22 20:10

Laurent LAPORTE