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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With