what is a function?
A function is code that is callable, this means I can write code to do something one time, and call the name for it to do that thing at a different time from when I write it.
functions can make code simpler, easier to read, test, reuse, maintain and improve - all the good things.
how to make a function
functions are made with
the def keyword
a name
parentheses and a colon at the end
the code that makes up the function (its body) comes after the colon
def name_of_function():
the body of the function
...
preview
I have these tests by the end of the chapter
1def test_making_a_function_w_pass():
2 def w_pass():
3 pass
4
5 assert w_pass() is None
6
7
8def test_making_a_function_w_return():
9 def w_return():
10 return
11
12 assert w_return() is None
13
14
15def test_making_a_function_w_return_none():
16 def w_return_none():
17 return None
18
19 assert w_return_none() is None
22def test_what_happens_after_functions_return():
23 def return_leaves_the_function():
24 return None
25 return 'only one way for this line to run'
26
27 assert return_leaves_the_function() is None
28
29
30def test_constant_function():
31 def constant():
32 return 'the same thing'
33
34 assert constant() == 'the same thing'
35
36
37# Exceptions seen
38# AssertionError
39# NameError
40# TypeError
questions about functions
start the project
I name this project
functionsI open a terminal
I use uv to make a directory for the project and initialize it
the terminal shows
Initialized project `functions` at `.../pumping_python/functions`then goes back to the command line.
I change directory to the project
the terminal shows I am in the
functionsfolder.../pumping_python/functionsI make a directory for the tests
mkdir teststhe terminal goes back to the command line.
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line.
I make an empty file for the tests in the
testsfoldertouch tests/test_functions.pyNew-Item tests/test_functions.pythe terminal goes back to the command line.
I open
test_functions.pyfrom thetestsfolderI add the first failing test to
test_functions.py1def test_failure(): 2 assert False is TrueI go back to the terminal
I use echo to add pytest to
requirements.txtecho "pytest" > requirements.txtI add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
I use Out-File to add pytest to
requirements.txt"pytest" | Out-File requirements.txt -Encoding UTF8I add pytest-watcher to the requirements file
"pytest-watcher" >> requirements.txt
I use uv to install pytest-watcher with the requirements file
the terminal shows that it installed pytest-watcher and its dependencies.
I add the new files and folders to git for tracking
the terminal goes back to the command line.
I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
I use pytest-watcher to run the tests automatically
the terminal is my friend, and shows AssertionError
======================== FAILURES ======================== ______________________ test_failure ______________________ def test_failure(): > assert False is True E assert False is True test_functions.py:2: AssertionError ================ short test summary info ================= FAILED test_functions.py::test_failure - assert False is True =================== 1 failed in X.YZs ====================if the terminal does not show the same error, then check if
your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyyou ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
and try
uv run pytest-watcher . --now --patterns "**/*.py"againI add AssertionError to the list of Exceptions seen, in
test_functions.py1def test_failure(): 2 assert False is True 3 4 5# Exceptions seen 6# AssertionErrorI change True to False in the assertion
1def test_failure(): 2 # assert False is True 3 assert False is False 4 5 6# Exceptions seen 7# AssertionErrorthe test passes.
test_making_a_function_w_pass
The simplest function I can make is with the pass keyword.
RED: make it fail
I change
test_failureto test_making_a_function_w_pass1def test_making_a_function_w_pass(): 2 w_pass 3 4 5# Exceptions seen 6# AssertionErrorthe terminal is my friend, and shows NameError
> w_pass E NameError: name 'w_pass' is not definedbecause Python does not know what I mean by
w_passsince I do not have a definition for it intest_functions.py.I add NameError to the list of Exceptions seen
what is a variable?
I can define a name in Python with a variable. A variable is a name that is used to point to an object. For example, in Mathematics we use x for any number.
Every time I use the name, Python uses the object I pointed the name to.
GREEN: make it pass
I change w_pass to a variable by pointing it to None (the simplest object)
1def test_making_a_function_w_pass():
2 # w_pass
3 w_pass = None
4
5
6# Exceptions seen
7# AssertionError
8# NameError
the test passes.
REFACTOR: make it better
I add an assertion
1def test_making_a_function_w_pass(): 2 # w_pass 3 w_pass = None 4 assert w_pass is 0 5 6 7# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert None is 0because
w_passpoints to None. Using substitutionw_pass = None assert w_pass is 0 assert None is 0which raises AssertionError since None is not the same object as an integer (whole number without decimals).
I change the assertion to make the statement True
1def test_making_a_function_w_pass(): 2 # w_pass 3 w_pass = None 4 # assert w_pass is 0 5 assert w_pass is None 6 7 8# Exceptions seenthe test passes.
how to call a function
I can call a function by placing parentheses (()) to the right of the name when I use it after it is defined.
name_of_function()
A call to a function gets the output of the function
name_of_function() -> output_object
RED: make it fail
I add parentheses to the assertion to call
w_passinside test_making_a_function_w_pass1def test_making_a_function_w_pass(): 2 # w_pass 3 w_pass = None 4 # assert w_pass is 0 5 # assert w_pass is None 6 assert w_pass() is None 7 8 9# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause the assertion calls
w_passwhich points to None and I cannot call None like a function. Using substitutionw_pass = None # point the name to the object w_pass() # call the name None() # substitute the value for the nameNone()raises TypeError.I add TypeError to the list of Exceptions seen
GREEN: make it pass
I use the def and pass keywords to make
w_passthe simplest function I can make1def test_making_a_function_w_pass(): 2 # w_pass 3 # w_pass = None 4 # assert w_pass is 0 5 # assert w_pass is None 6 def w_pass(): 7 pass 8 9 assert w_pass() is None 10 11 12# Exceptions seenThe assertion -
assert w_pass() is Nonechecks if the result of a call to thew_passfunction, is the same object as None.The function definition simply says pass and the test passes.
pass is a keyword that allows the function definition to follow Python language rules (the function must have a body).
The test passes because all functions return None by default, as if they have an invisible line that says return None, so I get None when I call
w_passw_pass() -> None └── def w_pass(): └── passwhich leads me to the next test, but first cleanup time.
REFACTOR: make it better
I remove the commented lines
1def test_making_a_function_w_pass(): 2 def w_pass(): 3 pass 4 5 assert w_pass() is None 6 7 8# Exceptions seen 9# AssertionError 10# NameError 11# TypeErrorI open another terminal then change directory to
functionscd functionsI add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
test_making_a_function_w_return
I can also make a function with a return statement.
RED: make it fail
I go back to the terminal where the tests are running
I add a new test with
w_return1def test_making_a_function_w_pass(): 2 def w_pass(): 3 pass 4 5 assert w_pass() is None 6 7 8def test_making_a_function_w_return(): 9 w_return 10 11 12# Exceptions seenthe terminal is my friend, and shows NameError
> w_return E NameError: name 'w_return' is not definedbecause Python does not know what I mean by
w_returnsince I do not have a definition for it intest_functions.py.
GREEN: make it pass
I change w_return to a variable by pointing it to None (the simplest object)
8def test_making_a_function_w_return():
9 # w_return
10 w_return = None
11
12
13# Exceptions seen
the test passes.
REFACTOR: make it better
I add an assertion for
w_returnthe terminal is my friend, and shows AssertionError
E assert None is 1because
w_returnpoints to None. Using substitutionw_return = None assert w_return is 1 assert None is 1which raises AssertionError since None is not the same object as an integer (whole number without decimals).
I change the assertion to make the statement True
the test passes.
I add parentheses to call
w_returninside test_making_a_function_w_returnthe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause the assertion calls
w_returnwhich points to None and I cannot call None like a function. Using substitutionw_return = None # point the name to the object w_return() # call the name None() # substitute the value for the nameNone()raises TypeError.I use the def and pass keywords to change
w_returnto the simplest function I can makethe test passes.
the return statement
The return keyword is used to define what a function gives as output when it is called.
functions are made with
the def keyword
a name
parentheses and a colon at the end
the code that makes up the function (its body)
the return statement to define the output when the function is called.
def name_of_function():
the body of the function
return
I change pass to a return statement with the return keyword
The assertion -
assert w_return() is Nonechecks if the result of a call tow_return, is the same object as None.The function definition simply says return and the test passes.
return is a keyword that defines what the function gives as output.
The test is still green because I get None when I call the
w_returnfunctionw_return() -> None └── def w_return(): └── return
I remove the commented lines
1def test_making_a_function_w_pass(): 2 def w_pass(): 3 pass 4 5 assert w_pass() is None 6 7 8def test_making_a_function_w_return(): 9 def w_return(): 10 return 11 12 assert w_return() is None 13 14 15# Exceptions seenI add a git commit message in the other terminal
the terminal shows a summary of the changes then goes back to the command line.
I can make a function with a return statement.
I have two functions with different statements, and the tests show that they both return None
def w_pass():
pass
def w_return():
return
their contents are different, their results are the same because “all functions return None by default, as if they have an invisible line that says return None”, which leads me to the next test.
test_making_a_function_w_return_none
I can make a function with a return statement that says exactly what the function returns.
def name_of_function():
the body of the function
return output
RED: make it fail
GREEN: make it pass
I change w_return_none to a variable by pointing it to None (the simplest object)
15def test_making_a_function_w_return_none():
16 # w_return_none
17 w_return_none = None
18
19
20# Exceptions seen
the test passes.
REFACTOR: make it better
I add an assertion for
w_return_nonethe terminal is my friend, and shows AssertionError
E assert None is 2because
w_return_nonepoints to None. Using substitutionw_return_none = None assert w_return_none is 2 assert None is 2which raises AssertionError since None is not the same object as an integer (a whole number without decimals).
I change the assertion to make the statement True
the test passes.
I add parentheses to call
w_return_noneinside test_making_a_function_w_return_nonethe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause the assertion calls
w_return_nonewhich points to None and I cannot call None like a function. Using substitutionw_return_none = None # point the name to the object w_return_none() # call the name None() # substitute the value for the nameNone()raises TypeError.I change
w_return_noneto a function with the def and return keywordsthe test passes.
I add None to the return statement of
w_return_nonethe test is still green.
I change None to
'something'the terminal is my friend, and shows AssertionError
AssertionError: assert 'something' is Nonebecause I get
'something'when I callw_return_nonew_return_none() -> 'something' └── def w_return_none(): └── return 'something'Using substitution
assert w_return_none() is None assert 'something' is Nonewhich raises AssertionError because a string (anything in quotes) is not None.
I undo the change
I remove the commented lines
I add a git commit message in the other terminal
the terminal shows a summary of the changes then goes back to the command line.
I can make a function with return None..
I have three functions with different statements, and the tests show that they all return None
def w_pass():
pass
def w_return():
return
def w_return_none():
return None
their contents are different, their results are the same because “all functions return None by default, as if they have an invisible line that says …”
I like to write my functions with explicit return statements, so that anyone can see what the function returns without having to think about it.
test_what_happens_after_functions_return
The return statement is the last thing to run in a function. No statements after it in a function will run when the function is called.
RED: make it fail
I go back to the terminal where the tests are running
I add a test to
test_functions.pythe terminal is my friend, and shows NameError
NameError: name 'return_leaves_the_function' is not definedbecause Python does not know what I mean by
return_leaves_the_functionsince I do not have a definition for it in this file, yet.
GREEN: make it pass
I change return_leaves_the_function to a variable by pointing it to None (the simplest object)
22def test_what_happens_after_functions_return():
23 # return_leaves_the_function
24 return_leaves_the_function = None
25
26
27# Exceptions seen
the test passes.
REFACTOR: make it better
I add an assertion
the terminal is my friend, and shows AssertionError
AssertionError: assert None is 'something'because
return_leaves_the_functionpoints to None. Using substitutionreturn_leaves_the_function = None assert return_leaves_the_function is 'something' assert None is 'something'which raises AssertionError since None is not the same object as a string.
I change the assertion to make the statement True
the test passes.
I add parentheses to call
return_leaves_the_functioninside test_what_happens_after_functions_returnthe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause the assertion calls
return_leaves_the_functionwhich points to None and I cannot call None like a function. Using substitutionreturn_leaves_the_function = None # point the name to the object return_leaves_the_function() # call the name None() # substitute the valueNone()raises TypeError.I change
return_leaves_the_functionto a function with the def and return keywordsthe test passes.
I add a return statement
the terminal is my friend, and shows AssertionError
E AssertionError: assert 'something' is Nonebecause I get
'something'when I callreturn_leaves_the_function. The return statement is the last thing to run in a function, it exits after the return statement.return_leaves_the_function() -> 'something' def return_leaves_the_function(): └── return 'something' return NoneUsing substitution
assert return_leaves_the_function() is None assert 'something' is Nonewhich raises AssertionError because a string is not None.
It never gets to
return Nonebecause it leaves afterreturn 'something'.The second return statement will never run. It is not reachable (this is called dead code).
This is why I can treat a call to a function as the object it returns.
I move
return None, to make it the first return statementthe test is green again.
I change the second return statement as a reminder
the second return statement is now like a comment, and the test is still green because the return statement is the last thing to run in a function.
I remove the commented lines
I add a git commit message in the other terminal
the terminal shows a summary of the changes then goes back to the command line.
test_constant_function
There are functions that always return the same thing when they are called. They are singleton or constant functions.
RED: make it fail
GREEN: make it pass
I change constant to a variable by pointing it to None
30def test_constant_function():
31 # constant
32 constant = None
33
34
35# Exceptions seen
the test passes.
REFACTOR: make it better
I add an assertion
the terminal is my friend, and shows AssertionError
AssertionError: assert None is 'the same thing'because
constantpoints to None. Using substitution since I can treat a call to a function as the object it returnsconstant = None assert constant is 'the same thing' assert None is 'the same thing'which raises AssertionError since None is not the same object as a string (anything in quotes).
I change the assertion to make the statement True
the test passes.
I add parentheses to call
constantthe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause the assertion calls
constantwhich points to None and I cannot call None like a function. Using substitution since I can treat a call to a function as the object it returnsconstant = None # point the name to the object constant() # call the name None() # substitute the value for the nameNone()raises TypeError.I change
constantto a function with the def and return keywordsthe test passes.
constant() -> None def constant(): └── return NoneI change the return statement
the terminal is my friend, and shows AssertionError
AssertionError: assert 'the same thing' is NoneI change the assertion to make it True
the test passes because I get
'the same thing'when I call theconstantfunctionconstant() -> 'the same thing' def constant(): └── return 'the same thing'I remove the commented lines
I add a git commit message in the other terminal
the terminal shows a summary of the changes then goes back to the command line.
A constant function always returns the same thing when called, I can use them in place of variables. The number of cases where they are faster than variables is pretty small. It is something like if the function is called less than 10 times (who’s counting?)
All the functions I have written so far return the same value every time they are called.
close the project
I close
test_functions.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
functionsthe terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I ran tests to show that I can make functions with the def, pass and return keywords.
code from the chapter
what is next?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.