Functions
Function is a series of statements which returns some value to a caller. It can also be passed zero or more arguments which may be used in the execution of the body.
It is callable object created by using either operator
def
orlambda
. Function is object as anything else in Python so you can assign them to variables, append to lists etc.
Methods are functions defined inside some class.
Functions are evaluated only when they are called.
Definition:
Call function (execute and get it's result):
Argument vs Parameter
Parameter is the function local variable it was declared with.
Arguments is the variable that was passed to the function during it's call.
An example:
Here:
x
is the parameter100500
is the argument
Note: Python's official documentation often refers to parameter as argument, so argument can be used in most cases as more general term.
Function always returns something. You can scpecify return value by operator return
. If not - None
will be returned.
Python code statement can't be empty. You can "do nothing" with operator pass
:
or even in this way (Python 3 only):
The function definition does not execute the function body.
The body will be executed only when the function is called:
🪄 Code:
📟 Output:
Function definition's execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called.
In simple words, if the function accesses an object which is not defined in it - it will look in current global area:
🪄 Code:
📟 Output:
But most often the function relies on the data passed to it:
🪄 Code:
📟 Output:
We can assign default argument:
🪄 Code:
📟 Output:
Using list comprehesion we can make this function very short
🪄 Code:
📟 Output:
Arguments definitions
Note that arguments are passed using call by value (where the value is always an object reference, not the value of the object
It is possible to define a function by using the following types of formal arguments:
Positional/Required arguments
Keyword arguments
Default arguments (can be positional or keyword)
Variable-length arguments (also called arbitrary argument lists)
Schematics of parameters:
/
works from Python 3.8
If /
and *
are not present in the function definition, arguments may be passed to a function by position or by keyword. We already saw these three types (positional, keyword and default).
An example of calling function using positional arguments
x
andy
:sum(10, 20)
An example of usage default argument
y
:sum(10)
An example of passing argument as keywords:
sum(x=10, y=6)
sum(y=6, x=10)
Required arguments
Required arguments are the arguments passed to a function in correct positional order (that's why they also known as positional). The number of arguments in the function call should match exactly with the function definition.
🪄 Code:
📟 Output:
Keyword arguments
Passing arguments as key=value pair in accordance with function definition. No changes needed for function. Order of arguments when calling a function doesn't matter.
For previous example:
🪄 Code:
📟 Output:
Default arguments
A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.
🪄 Code:
📟 Output:
Variable-length arguments
You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.
An asterisk (
*
) is placed before the variable name that holds the values of all non-keyword variable arguments.Two asterisks (
**
) are placed before the variable name that holds dictionary with keyword-variable arguments.
🪄 Code:
📟 Output:
Order
Order of arguments of different types matters:
Positionals:
required/positional
default positional
variable-length non-keyword
Keywords:
keyword
default keyword
variable-length keyword
This allows avoid difficulties with understanding which argument goes to which variable.
🪄 Code:
📟 Output:
🪄 Code:
📟 Output:
Positional-Only parameters
From Python 3.8 there is special syntax to mark some arguments as positional-only. Such arguments are placed before a
/
symbol, the rest of the parameters are not positional-only (they can be positional-or-keyword or keyword-only).
The thing is - that many built-in functions implemented in C already accept only positional arguments:
🪄 Code:
📟 Output:
This is useful in many case, for example int
has this help:
But calling int(x=100)
is ugly and incorrect while int("100")
looks natural.
So now (from 3.8) Python has the same syntax option as in C allowing to prohibit keyword arguments:
Keyword-only arguments
There is a special syntax to mark some arguments as keyword-only. Those parameters go after
*
symbol in the arguments list.
Parameters marked as keyword-only can be passed only by keyword.
Consider this function:
There is a lot of ways to call this function, and not all calls are understandable in terms of which value was passed to each argument:
The keyword-only parameters definition can be used to avoid the issue what it is not possible to understand what argument was set:
🪄 Code:
📟 Output:
## Annotations
Mostly decorative optional feature. Used to generate more understandable help and documentation, add support for this in IDE.
More examples:
Usecases:
Providing typing information
Type checking
Let IDEs show what types a function expects and returns
Foreign-language bridges
Database query mapping
Other information
Documentation for parameters and return values
Last updated