Python basics, part 4 of 6
Python Functions
A function is a named block of code that you write once and run whenever you need it. This page is a Python 3 rewrite of the Pythofrank lessons on functions and map from February 2017. The original examples are kept and updated from Python 2.7, and the parts a beginner needs next are added: arguments, scope, lambda, recursion and type hints.
Defining and calling
def defines a function: a name, a list of parameters in brackets, a colon, then an indented body. Nothing in the body runs until the function is called by writing its name followed by brackets.
Terms: a parameter is the name in the definition. An argument is the value you pass in the call. The 2017 lesson named this function sum. That works, but it hides Python's built-in sum(), so it is called add here.
Return values
return ends the function and sends a value back to the place it was called from. A function that reaches its end without return gives back None, Python's value for "nothing".
A function can have more than one return. The first one reached ends it. To return several values, separate them with commas. They travel back together as one tuple, which the caller can unpack into separate names.
Difference: print() shows a value to a person. return gives a value to the rest of the program. A function that only prints cannot be used in a calculation.
Positional and keyword arguments
Arguments are matched to parameters by position: first to first, second to second. You can also name the parameter in the call. Named arguments are called keyword arguments and can be given in any order.
Rule: in a call, positional arguments come first and keyword arguments after them. Use keywords when the meaning of a value is not obvious from the call alone.
Default values
A parameter can have a default value, written with = in the definition. The caller may then leave that argument out.
Rule: parameters with defaults must come after the ones without. def open_account(balance=0, owner): is a syntax error.
The mutable default trap
A default value is created once, when the def line runs, not on every call. If the default is a list or a dictionary, which can be changed in place (they are mutable), every call that uses the default shares the same object.
The fix is to use None as the default and make a new list inside the function.
Rule: never use a list, dictionary or set as a default value. Numbers, strings, tuples and None cannot be changed in place, so they are safe.
*args and **kwargs
Sometimes you do not know how many arguments will come. A parameter with one star collects all extra positional arguments into a tuple. A parameter with two stars collects all extra keyword arguments into a dictionary. The names args and kwargs are only a habit. The stars do the work.
The stars also work the other way round. In a call, * spreads a list over positional parameters and ** spreads a dictionary over keyword parameters.
Keyword-only arguments
Parameters written after a bare * can only be passed by name. This protects options, such as on and off switches, from being set by accident.
Why: withdraw(1000, 1500, True) tells the reader nothing about what True means. Forcing the name makes every call explain itself.
Docstrings
A string written as the first statement of a function is its docstring, short for documentation string. It says what the function does. Python stores it in __doc__, and help() and code editors show it.
Convention: use triple double quotes, start with one short line that says what the function returns or does, and add details after a blank line only when they are needed. In interactive mode, help(simple_interest) prints the same text.
Scope: local and global
Scope is the part of a program where a name can be seen. A name assigned inside a function is local: it exists only while that call runs. A name assigned at the top level of the file is global. A function can read a global name, but assigning to that name inside the function makes a new local one.
The global statement tells Python that an assignment inside the function should change the global name.
Advice: use global rarely. A function that takes values as arguments and returns its result is easier to test and to reuse than one that quietly changes a variable somewhere else.
Functions inside functions and nonlocal
A function can be defined inside another function. The inner one can read the names of the outer one. To assign to such a name, declare it nonlocal. The inner function keeps those names alive even after the outer function has returned. This is called a closure.
Lookup order: Python searches for a name in the local scope first, then in any enclosing functions, then in the globals, then in the built-ins. This order is often called LEGB.
Functions as values
A function is a value like a number or a string. Without brackets, its name refers to the function itself. You can give it a second name, put it in a list or dictionary, and pass it to another function.
lambda
lambda writes a small function without a name, in one line. After the word come the parameters, then a colon, then one expression whose value is returned. It is meant for short functions that are passed straight to another function.
Limit: a lambda holds one expression only: no statements, no assignments, no several lines. If you need a name for it or more than one line, use def.
map
map(func, items) calls func on every item and gives the results in the same order. In Python 3 it returns a lazy map object that produces values only when asked, so wrap it in list() to see them.
Give map two sequences and the function receives one item from each. It stops when the shorter sequence ends.
The function may return anything, including a list. Here each number becomes its multiplication table.
Python 2: in the 2017 lesson map() returned a list directly, and a shorter second sequence was filled up with None. Python 3 returns a lazy object and stops at the shorter sequence. A map object can be looped over only once.
filter and sorted with key
filter(func, items) keeps the items for which func returns a true value. Like map, it is lazy.
sorted() returns a new sorted list. Its key argument is a function that is called on each item, and the items are ordered by what that function returns. min() and max() accept key too.
Comprehensions instead of map and filter
A list comprehension does the work of map and filter in one expression, and usually reads better because no lambda is needed. The part before for is the map step. The if at the end is the filter step.
Which one: use a comprehension when you would otherwise write a lambda. map is still neat when the function already exists, as in list(map(int, ["1", "2", "3"])).
Recursion
A function that calls itself is recursive. It needs two parts: a base case that returns without another call, and a step that calls the function again with a smaller problem. Without a base case the calls would never stop.
Limit: Python stops a chain of calls that grows too deep, by default at about 1000 levels, and raises RecursionError. For plain counting or adding, a loop is simpler and faster. Recursion fits data that is nested, such as folders inside folders.
Type hints
A type hint notes which type a parameter should be and which type the function returns. Write name: type for a parameter and -> type before the colon for the return value.
Concept: Python does not enforce hints. They are for the reader, for editors that complete and check code as you type, and for separate checking tools such as mypy.
Quick reference
| You want to | Write |
|---|---|
| Define a function | def add(a, b): |
| Give a result back | return a + b |
| Return several values | return balance, True, then balance, done = withdraw(...) |
| Pass an argument by name | transfer("Subhash", receiver="Sandhya", amount=500) |
| Make an argument optional | def open_account(owner, balance=0): |
| Default to an empty list safely | history=None, then if history is None: history = [] |
| Accept any number of arguments | def total(*amounts):, def describe(**details): |
| Force an argument to be named | def withdraw(balance, amount, *, allow_overdraft=False): |
| Document a function | """Return ...""" as its first statement |
| Change a global name | global transactions |
| Change a name of the outer function | nonlocal balance |
| Write a one-line function | lambda n: n ** 2 |
| Change every item | [n ** 2 for n in nums] or list(map(square, nums)) |
| Keep some items | [n for n in nums if n > 0] or list(filter(func, nums)) |
| Sort by a rule | sorted(accounts, key=lambda account: account[1]) |
| Note the expected types | def double(n: int) -> int: |
The 2017 originals, with their Python 2.7 code, are still online: Functions and The map function.
Next, bundle data and the functions that work on it into one unit in Python Classes.