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
15
16
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# ModuleNotFoundError
33# AttributeError
34# NameError
35# TypeError
questions about AttributeError
Questions to think about as I go through the chapter
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 a Python file for the tests in the
testsdirectorytouch tests/test_attribute_error.pyNew-Item tests/test_attribute_error.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.pyI delete all the text then add the first failing test to
test_attribute_error.py1def test_failure(): 2 assert False is TrueI go back to the terminal to make a requirements file for the Python packages I need
echo "pytest" > requirements.txtthe terminal goes back to the command line.
I add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
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
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
test_attribute_error.py1import src.attribute_errorthe terminal is my friend, and shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src'because Python cannot find anything named
srcin this project.I add ModuleNotFoundError to the list of Exceptions seen
9# Exceptions seen 10# AssertionError 11# ModuleNotFoundErrorI open a new terminal then change directories to
attribute_errorcd attribute_errorI use mkdir to make a folder named
srcin the projectmkdir srcI go back to the terminal where the tests are running
I go to
test_attribute_error.pyand use ctrl/command+s on the keyboard to run the test again. The terminal shows ModuleNotFoundErrorModuleNotFoundError: No module named 'src.attribute_error'because there is nothing named
attribute_errorin thesrcfolderI go to the second terminal
I use touch to make
attribute_error.pyin thesrcfoldertouch src/attribute_error.pyI go to the terminal where the tests are running and it shows the test is green again.
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 when
src.attribute.variable_00runs, Python follows this pathsrc └── attribute_error.py └── variable_00 # does not exist in attribute_error.pywhich raises AttributeError since there is nothing named
variable_00inattribute_error.pyin thesrcfolder, it is empty.I add AttributeError to the list of Exceptions seen, in
test_attribute_error.py8# Exceptions seen 9# AssertionError 10# ModuleNotFoundError 11# AttributeError
GREEN: make it pass
I open
attribute_error/__init__.pyfrom thesrcfolderI add
variable_00toattribute_error.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
test_attribute_error.py8# Exceptions seen 9# AssertionError 10# ModuleNotFoundError 11# AttributeError 12# NameErrorI point
variable_00to None to define it, inattribute_error.py1# variable_00 2variable_00 = Nonethe test passes because
Python brings in an object for the
attribute_error.pyfile from thesrcfolder so I can use it intest_attribute_error.pyassrc.attribute_errorwhenimport src.attribute_errorruns.Python looks at what the
variable_00variable from the object it imported for theattribute_error.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.py └── variable_00 = Nonevariable_00is something inattribute_error, in this case it is a variable inattribute_errorattribute_erroris something insrc, in this case it isattribute_error.py(a module) in thesrcfoldersrcis something Python can import (a module, Python package or folder)variable_00is now an attribute/property ofattribute_error.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_01totest_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_01toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_01src.attribute_error.variable_01 src └── attribute_error.py └── variable_01 = variable_00I add a statement for
variable_02totest_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 inattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_02src.attribute_error.variable_02 src └── attribute_error.py └── variable_02 = variable_01I add a line for
variable_03totest_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_03toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_03src.attribute_error.variable_03 src └── attribute_error.py └── variable_03 = variable_02I add a line for
variable_04totest_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_04toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_04src.attribute_error.variable_04 src └── attribute_error.py └── variable_04 = variable_03I add a line for
variable_05totest_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_05toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_05src.attribute_error.variable_05 src └── attribute_error.py └── variable_05 = variable_04I add a line for
variable_06totest_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_06toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_06src.attribute_error.variable_06 src └── attribute_error.py └── variable_06 = variable_05I add a line for
variable_07totest_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_07toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_07src.attribute_error.variable_07 src └── attribute_error.py └── variable_07 = variable_06I add a line for
variable_08totest_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_08toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_08src.attribute_error.variable_08 src └── attribute_error.py └── variable_08 = variable_07I add a line for
variable_09totest_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_09toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_09src.attribute_error.variable_09 src └── attribute_error.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
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_00inattribute_error.pyin thesrcfolder.
GREEN: make it pass
I add the name and point it to None in
attribute_error.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
test_attribute_error.py21# Exceptions seen 22# AssertionError 23# ModuleNotFoundError 24# AttributeError 25# NameError 26# TypeErrorI change the variable to a function in
attribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_00()src.attribute_error.function_00() src └── attribute_error.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 intest_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_01toattribute_error.py13def function_00(): return variable_09 14def function_01(): return function_00()the test passes because
function_01is now an attribute ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_01()src.attribute_error.function_01() src └── attribute_error.py └── def function_01(): return function_00()I add a call to
function_02from test_attribute_error_w_functions intest_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_02toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_02()src.attribute_error.function_02() src └── attribute_error.py └── def function_02(): return function_01()I add a call to
function_03from test_attribute_error_w_functions intest_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_03toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_03()src.attribute_error.function_03() src └── attribute_error.py └── def function_03(): return function_02()I add a call to
function_04from test_attribute_error_w_functions intest_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_04toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_04()src.attribute_error.function_04() src └── attribute_error.py └── def function_04(): return function_03()I add a call to
function_05from test_attribute_error_w_functions intest_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_05toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_05()src.attribute_error.function_05() src └── attribute_error.py └── def function_05(): return function_04()I add a call to
function_06from test_attribute_error_w_functions intest_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_06toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_06()src.attribute_error.function_06() src └── attribute_error.py └── def function_06(): return function_05()I add a call to
function_07from test_attribute_error_w_functions intest_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_07toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_07()src.attribute_error.function_07() src └── attribute_error.py └── def function_07(): return function_06()I add a call to
function_08from test_attribute_error_w_functions intest_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_08toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_08()src.attribute_error.function_08() src └── attribute_error.py └── def function_08(): return function_07()I add a call to
function_09from test_attribute_error_w_functions intest_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_09toattribute_error.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 ofattribute_error.pyin thesrcfolder, and I can call it from outside the file withsrc.attribute_error.function_09()src.attribute_error.function_09() src └── attribute_error.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
attribute_error.pyandtest_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
I also saw these Exceptions
My problem with the tests is that they all show the correct way to use attributes I made in attribute_error.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?
Would you like to see me separate the tests and functions in the type_error project?
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.