what causes AttributeError?
AttributeError happens when a name that is NOT in an object (everything in Python is an object) is used.
what is an attribute?
An attribute is a name (variable?) for something that belongs to an object (a class), for example, a human being has attributes like height, weight, sex and color, they are also known as properties.
preview
I have these tests by the end of the chapter
1import src.attribute_error
2
3
4def test_attribute_error_w_variables():
5 src.attribute_error.variable_00
6 src.attribute_error.variable_01
7 src.attribute_error.variable_02
8 src.attribute_error.variable_03
9 src.attribute_error.variable_04
10 src.attribute_error.variable_05
11 src.attribute_error.variable_06
12 src.attribute_error.variable_07
13 src.attribute_error.variable_08
14 src.attribute_error.variable_09
17def test_attribute_error_w_functions():
18 src.attribute_error.function_00()
19 src.attribute_error.function_01()
20 src.attribute_error.function_02()
21 src.attribute_error.function_03()
22 src.attribute_error.function_04()
23 src.attribute_error.function_05()
24 src.attribute_error.function_06()
25 src.attribute_error.function_07()
26 src.attribute_error.function_08()
27 src.attribute_error.function_09()
28
29
30# Exceptions seen
31# AssertionError
32# AttributeError
33# NameError
34# TypeError
questions about AttributeError
start the project
I name this project
attribute_errorI open a terminal
I change directory to the
attribute_errorfolder in thepumping_pythonfoldercd attribute_errorthe terminal shows
cd: no such file or directory: attribute_errorthere is no folder with the name
attribute_errorin this folder.I use uv to make a directory for the project and initialize it
uv init attribute_errorthe terminal shows
Initialized project `attribute-error` at `.../pumping_python/attribute_error`then goes back to the command line.
I change directory to the project
cd attribute_errorthe terminal shows I am in the
attribute_errorfolder.../pumping_python/attribute_errorI 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_attribute_error.pyNew-Item tests/test_attribute_error.pythe terminal goes back to the command line.
I open
test_attribute_error.pyfrom thetestsfolderI add the first failing test to
tests/test_attribute_error.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
uv add --requirement requirements.txtthe terminal shows that it installed pytest-watcher and its dependencies.
I add the new files and folder to git for tracking
git add .the terminal goes back to the command line.
I add a git commit message
git commit -am 'setup project'the terminal shows a summary of the changes then goes back to the command line.
I use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
======================== FAILURES ======================== ______________________ test_failure ______________________ def test_failure(): > assert False is True E assert False is True test_attribute_error.py:2: AssertionError ================ short test summary info ================= FAILED test_attribute_error.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 . --nowagainI add AssertionError to the list of Exceptions seen, in
tests/test_attribute_error.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.
I add an import statement at the top of
tests/test_attribute_error.py1import src.attribute_error
test_attribute_error_w_variables
AttributeError happens when I use a variable that does not exist in an object.
RED: make it fail
I change test_failure to
test_attribute_error_w_variables1import src.attribute_error 2 3 4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 7 8# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_00'because Python follows this path when
src.attribute_error.variable_00runssrc.attribute_error.variable_00 └── src/ └── attribute_error/ └── __init__.py └── variable_00 # does not existwhich raises AttributeError since there is nothing named
variable_00in theattribute_errormodule in thesrcfolder.I add AttributeError to the list of Exceptions seen, in
tests/test_attribute_error.py8# Exceptions seen 9# AssertionError 10# AttributeError
GREEN: make it pass
I open
__init__.pyfrom theattribute_errorfolder in thesrcfolderI delete the text in the file then add
variable_00tosrc/attribute_error/__init__.py1variable_00the terminal is my friend, and shows NameError
NameError: name 'variable_00' is not definedbecause I used a name that is not defined in this file.
I add NameError to the list of Exceptions seen, in
tests/test_attribute_error.py8# Exceptions seen 9# AssertionError 10# AttributeError 11# NameErrorI point
variable_00to None to define it, insrc/attribute_error/__init__.py1# variable_00 2variable_00 = Nonethe test passes because
Python brings in an object for the
src/attribute_error/__init__.pyfile from thesrcfolder so I can use it intests/test_attribute_error.pyassrc.attribute_errorwhenimport src.attribute_errorruns.Python looks at what the
variable_00variable from the object it imported for thesrc/attribute_error/__init__.pyfile from thesrcfolder (src.attribute_error) points to whensrc.attribute_error.variable_00runs.
I think of
src.attribute_error.variable_00like an addresssrc.attribute_error.variable_00 └── src/ └── attribute_error/ └── __init__.py └── variable_00 = Nonevariable_00is something inattribute_error, in this case it is a variable inattribute_errorattribute_erroris something insrc, in this case it issrc/attribute_error/__init__.py(a module) in thesrcfoldersrcis something Python can import (a module, Python package or folder)variable_00is now an attribute/property ofsrc/attribute_error/__init__.pyin thesrcfolder. I can use it from outside the file withsrc.attribute_error.variable_00
REFACTOR: make it better
I add a statement for
variable_01totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 8 9# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_01'. Did you mean: 'variable_00'?I add
variable_01tosrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3variable_01the terminal is my friend, and shows NameError
NameError: name 'variable_01' is not definedI point it to None to define it
1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00the test passes because
variable_01is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_01src.attribute_error.variable_01 └── src/ └── attribute_error/ └── __init__.py └── variable_01 = variable_00I add a statement for
variable_02totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 9 10# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_02'. Did you mean: 'variable_00'?I add
variable_02and point it to None insrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00 5variable_02 = variable_01the test passes because
variable_02is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_02src.attribute_error.variable_02 └── src/ └── attribute_error/ └── __init__.py └── variable_02 = variable_01I add a line for
variable_03totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 10 11# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_03'. Did you mean: 'variable_00'?I add
variable_03tosrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00 5variable_02 = variable_01 6variable_03 = variable_02the test passes because
variable_03is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_03src.attribute_error.variable_03 └── src/ └── attribute_error/ └── __init__.py └── variable_03 = variable_02I add a line for
variable_04totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 11 12# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_04'. Did you mean: 'variable_00'?I add
variable_04tosrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00 5variable_02 = variable_01 6variable_03 = variable_02 7variable_04 = variable_03the test passes because
variable_04is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_04src.attribute_error.variable_04 └── src/ └── attribute_error/ └── __init__.py └── variable_04 = variable_03I add a line for
variable_05totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 12 13# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_05'. Did you mean: 'variable_00'?I add
variable_05tosrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00 5variable_02 = variable_01 6variable_03 = variable_02 7variable_04 = variable_03 8variable_05 = variable_04the test passes because
variable_05is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_05src.attribute_error.variable_05 └── src/ └── attribute_error/ └── __init__.py └── variable_05 = variable_04I add a line for
variable_06totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 src.attribute_error.variable_06 12 13 14# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_06'. Did you mean: 'variable_00'?I add
variable_06tosrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00 5variable_02 = variable_01 6variable_03 = variable_02 7variable_04 = variable_03 8variable_05 = variable_04 9variable_06 = variable_05the test passes because
variable_06is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_06src.attribute_error.variable_06 └── src/ └── attribute_error/ └── __init__.py └── variable_06 = variable_05I add a line for
variable_07totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 src.attribute_error.variable_06 12 src.attribute_error.variable_07 13 14 15# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_07'. Did you mean: 'variable_00'?I add
variable_07tosrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00 5variable_02 = variable_01 6variable_03 = variable_02 7variable_04 = variable_03 8variable_05 = variable_04 9variable_06 = variable_05 10variable_07 = variable_06the test passes because
variable_07is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_07src.attribute_error.variable_07 └── src/ └── attribute_error/ └── __init__.py └── variable_07 = variable_06I add a line for
variable_08totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 src.attribute_error.variable_06 12 src.attribute_error.variable_07 13 src.attribute_error.variable_08 14 15 16# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_08'. Did you mean: 'variable_00'?I add
variable_08tosrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00 5variable_02 = variable_01 6variable_03 = variable_02 7variable_04 = variable_03 8variable_05 = variable_04 9variable_06 = variable_05 10variable_07 = variable_06 11variable_08 = variable_07the test passes because
variable_08is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_08src.attribute_error.variable_08 └── src/ └── attribute_error/ └── __init__.py └── variable_08 = variable_07I add a line for
variable_09totests/test_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 src.attribute_error.variable_06 12 src.attribute_error.variable_07 13 src.attribute_error.variable_08 14 src.attribute_error.variable_09 15 16 17# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_09'. Did you mean: 'variable_00'?I add
variable_09tosrc/attribute_error/__init__.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = variable_00 5variable_02 = variable_01 6variable_03 = variable_02 7variable_04 = variable_03 8variable_05 = variable_04 9variable_06 = variable_05 10variable_07 = variable_06 11variable_08 = variable_07 12variable_09 = variable_08the test passes because
variable_09is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_09src.attribute_error.variable_09 └── src/ └── attribute_error/ └── __init__.py └── variable_09 = variable_08I remove the commented lines
1variable_00 = None 2variable_01 = variable_00 3variable_02 = variable_01 4variable_03 = variable_02 5variable_04 = variable_03 6variable_05 = variable_04 7variable_06 = variable_05 8variable_07 = variable_06 9variable_08 = variable_07 10variable_09 = variable_08I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_variables'
test_attribute_error_w_functions
RED: make it fail
I go back to the terminal where the tests are running
I add a test with a function call, to
tests/test_attribute_error.py14 src.attribute_error.variable_09 15 16 17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 20 21# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_00'because there is nothing named
function_00insrc/attribute_error/__init__.pyin thesrcfolder.
GREEN: make it pass
I add the name and point it to None in
src/attribute_error/__init__.py10variable_09 = variable_08 11 12 13function_00 = variable_09the terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause I cannot call None like a function.
I add TypeError to the list of Exceptions seen, in
tests/test_attribute_error.py21# Exceptions seen 22# AssertionError 23# AttributeError 24# NameError 25# TypeErrorI change the variable to a function in
src/attribute_error/__init__.py10variable_09 = variable_08 11 12 13# function_00 = variable_09 14def function_00(): return variable_09the test passes because
function_00is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_00()src.attribute_error.function_00() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_00(): return variable_09
REFACTOR: make it better
I remove the commented line
1variable_09 = variable_08 2 3 4def function_00(): return variable_09time to make it a drill.
I add a call to
function_01from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 21 22# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_01'. Did you mean: 'function_00'?I add a definition for
function_01tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00()the test passes because
function_01is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_01()src.attribute_error.function_01() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_01(): return function_00()I add a call to
function_02from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 22 23# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_02'. Did you mean: 'function_00'?I add a definition for
function_02tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00() 15def function_02(): return function_01()the test passes because
function_02is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_02()src.attribute_error.function_02() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_02(): return function_01()I add a call to
function_03from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 src.attribute_error.function_03() 22 23 24# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_03'. Did you mean: 'function_00'?I add a definition for
function_03tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00() 15def function_02(): return function_01() 16def function_03(): return function_02()the test passes because
function_03is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_03()src.attribute_error.function_03() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_03(): return function_02()I add a call to
function_04from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 src.attribute_error.function_03() 22 src.attribute_error.function_04() 23 24 25# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_04'. Did you mean: 'function_00'?I add a definition for
function_04tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00() 15def function_02(): return function_01() 16def function_03(): return function_02() 17def function_04(): return function_03()the test passes because
function_04is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_04()src.attribute_error.function_04() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_04(): return function_03()I add a call to
function_05from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 src.attribute_error.function_03() 22 src.attribute_error.function_04() 23 src.attribute_error.function_05() 24 25 26# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_05'. Did you mean: 'function_00'?I add a definition for
function_05tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00() 15def function_02(): return function_01() 16def function_03(): return function_02() 17def function_04(): return function_03() 18def function_05(): return function_04()the test passes because
function_05is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_05()src.attribute_error.function_05() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_05(): return function_04()I add a call to
function_06from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 src.attribute_error.function_03() 22 src.attribute_error.function_04() 23 src.attribute_error.function_05() 24 src.attribute_error.function_06() 25 26 27# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_06'. Did you mean: 'function_00'?I add a definition for
function_06tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00() 15def function_02(): return function_01() 16def function_03(): return function_02() 17def function_04(): return function_03() 18def function_05(): return function_04() 19def function_06(): return function_05()the test passes because
function_06is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_06()src.attribute_error.function_06() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_06(): return function_05()I add a call to
function_07from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 src.attribute_error.function_03() 22 src.attribute_error.function_04() 23 src.attribute_error.function_05() 24 src.attribute_error.function_06() 25 src.attribute_error.function_07() 26 27 28# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_07'. Did you mean: 'function_00'?I add a definition for
function_07tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00() 15def function_02(): return function_01() 16def function_03(): return function_02() 17def function_04(): return function_03() 18def function_05(): return function_04() 19def function_06(): return function_05() 20def function_07(): return function_06()the test passes because
function_07is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_07()src.attribute_error.function_07() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_07(): return function_06()I add a call to
function_08from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 src.attribute_error.function_03() 22 src.attribute_error.function_04() 23 src.attribute_error.function_05() 24 src.attribute_error.function_06() 25 src.attribute_error.function_07() 26 src.attribute_error.function_08() 27 28 29# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_08'. Did you mean: 'function_00'?I add a definition for
function_08tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00() 15def function_02(): return function_01() 16def function_03(): return function_02() 17def function_04(): return function_03() 18def function_05(): return function_04() 19def function_06(): return function_05() 20def function_07(): return function_06() 21def function_08(): return function_07()the test passes because
function_08is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_08()src.attribute_error.function_08() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_08(): return function_07()I add a call to
function_09from test_attribute_error_w_functions intests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 src.attribute_error.function_03() 22 src.attribute_error.function_04() 23 src.attribute_error.function_05() 24 src.attribute_error.function_06() 25 src.attribute_error.function_07() 26 src.attribute_error.function_08() 27 src.attribute_error.function_09() 28 29 30# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_09'. Did you mean: 'function_00'?I add a definition for
function_09tosrc/attribute_error/__init__.py13def function_00(): return variable_09 14def function_01(): return function_00() 15def function_02(): return function_01() 16def function_03(): return function_02() 17def function_04(): return function_03() 18def function_05(): return function_04() 19def function_06(): return function_05() 20def function_07(): return function_06() 21def function_08(): return function_07() 22def function_09(): return function_08()the test passes because
function_09is now an attribute ofsrc/attribute_error/__init__.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_09()src.attribute_error.function_09() -> None └── src/ └── attribute_error/ └── __init__.py └── def function_09(): return function_08()I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_functions'
close the project
I close
src/attribute_error/__init__.pyandtests/test_attribute_error.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
attribute_errorcd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I ran tests for AttributeError that showed
My problem with the tests is that they all show the correct way to use attributes I made in src/attribute_error/__init__.py. If someone reads the file or runs it, there is no way for them to know how the code relates to AttributeError unless they go through the process with me, there has to be a better way.
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.