python arguments cheatsheet functions in python

Python cheatsheet: function and method parameters

Advanced things about function parameters, during function definition and call. Positional vs keyword arguments, dynamic arguments, …

None of the given code examples contain any type hinting. While I am in favor of them, this is not the subject of this article.

By default, two kind of parameters

As you probably already know, by default there are two ways of passing arguments to a function or method:

  • positional arguments: they are given in the same order in the function call than during its definition. The order of the arguments does matter.
  • keyword arguments: they are given using both the parameter name and its value. The order of the arguments does not matter.

You can also use both, that is to say call a method using both positional and keyword arguments. You must start by using positional arguments, and then finish with keyword arguments. If you don’t follow this rule you will get a “nice” SyntaxError: positional argument follows keyword argument

All those function calls are equivalent.

All those function calls are equivalent.

How can I force the use of one of the parameter types?

It can be a good practice to force giving mandatory parameters as positional ones and optional parameters (with default values) as keyword ones.

Force parameters to be given by position

To ensure parameters can only be given using position, you can use a forward slash ( / ) in the function signature. All parameters at the left side of the forward slash will then only be authorized to be given as positional-arguments.

Using forward slash to force using positional arguments

Using forward slash to force using positional arguments

Force parameters to be given by keywords

To ensure parameters can only be given using keywords, you can use an asterisk ( * ) in the function signature. All parameters at the right side of the asterisk will then only be authorized to be given as keyword-arguments.

Using asterisk to force using keyword arguments

Using asterisk to force using keyword arguments

Using both methods

You can also force first parameters to be given as positional-arguments and the others as keyword-arguments by using both the forward slash and the asterisk.

Using both forward slash and asterisk

Using both forward slash and asterisk

You can also have non restricted parameters in the middle of the forward slash and the asterisk.

Using both forward slash and asterisk

Using both forward slash and asterisk

Parameters with default values / Optional parameters

How to declare them?

Declaring a default value for a parameter is like giving a keyword-parameter, it uses the equal sign ( = )

Greetings method with optional parameter target

Greetings method with optional parameter target

Do not forget that mandatory parameters (without default values) must all be declared BEFORE optional parameters.

Non-default arguments must be declared first

Non-default arguments must be declared first

Be cautious of your default values

When you choose your default values you should use only scalar values (not object instances). The default values are created once and for all when the function is defined, that is to say all calls will share the same instance which can bring problems.

A small example is better than lots of words:

As you can see, the second call to my_function didn’t result in the same print than the first one.

To prevent this case, prefer always using a scalar, like None, as the default value and instantiate the object/list/dict inside the function if None is given

Going further with arguments

Variable number of arguments in function definition

Two notations are available while declaring a function to have a variable number of arguments.

For positional-parameters, you can define a parameter prefixed by a single asterisk ( * ) (this parameter is often named args: *args). It is the last positional arguments. (All parameters defined after it will only be usable using keywords)

Parameters after *args are keyword-only

Parameters after *args are keyword-only

For keyword-parameters, you can define a parameter prefixed by two asterisks ( ** ) (often named kwargs: **kwargs). It MUST be the last argument and it will capture all arguments given to the function that are not already declared in the signature.

*args is a tuple while **kwargs is a dict

*args is a tuple while **kwargs is a dict

Printing the values of *args and **kwargs

Printing the values of *args and **kwargs

*args and **kwargs can sometime be used to ignore any additional unused arguments

Calling a function with a sequence of values and/or a dict

When calling a function you can expand collections to give arguments, that is to say extract positional arguments from a single sequence and keyword arguments from a dictionary.

Expanding collections to give arguments

Expanding collections to give arguments

Argument resolution and class inheritance

Variable arguments are often used while subclassing a class in order to forward arguments to the parent constructor without having to explicitly capture them.

That’s all folks!

I hope you will have learn one thing or two while reading this article!