what causes AttributeError?
AttributeError is raised when I use a name that is NOT in an object that exists.
what is an attribute?
An attribute is a name for something that belongs to an object, for example, a human being has attributes like height, weight, sex and color, they are also known as properties.
preview
These are the tests I have by the end of the chapter
1import unittest
2import src.attribute_error
3
4
5class TestAttributeError(unittest.TestCase):
6
7 def test_attribute_error_w_variables(self):
8 src.attribute_error.variable_00
9 src.attribute_error.variable_01
10 src.attribute_error.variable_02
11 src.attribute_error.variable_03
12
13 def test_attribute_error_w_functions(self):
14 src.attribute_error.function_00()
15 src.attribute_error.function_01()
16 src.attribute_error.function_02()
17 src.attribute_error.function_03()
18
19 def test_attribute_error_w_class_attributes(self):
20 src.attribute_error.AClass.attribute_00
21 src.attribute_error.AClass.attribute_01
22 src.attribute_error.AClass.attribute_02
23 src.attribute_error.AClass.attribute_03
24
25 def test_attribute_error_w_class_methods(self):
26 src.attribute_error.AClass.method_00()
27 src.attribute_error.AClass.method_01()
28 src.attribute_error.AClass.method_02()
29 src.attribute_error.AClass.method_03()
30
31
32# Exceptions seen
33# AssertionError
34# AttributeError
35# NameError
36# TypeError
37# SyntaxError
start the project
I name this project
attribute_errorI open a terminal
then I make a directory for the project
mkdir attribute_errorthe terminal goes back to the command line
.../pumping_pythonI change directory to the project
cd attribute_errorthe terminal shows I am now in the
attribute_errorfolder.../pumping_python/attribute_errorI make a folder for the source code
mkdir srcthe terminal goes back to the command line
.../pumping_python/attribute_errorI use touch to make an empty file for the program in the
srcfoldertouch src/attribute_error.pyAttention
on Windows without Windows Subsystem for Linux use
New-Item src/attribute_error.pyinstead oftouch src/attribute_error.pyNew-Item src/attribute_error.pythe terminal goes back to the command line
.../pumping_python/attribute_errorI make a directory for the tests
mkdir teststhe terminal goes back to the command line
I use touch to make an empty file in the
testsfolder to tell Python that it is a Python packageAttention
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyAttention
on Windows without Windows Subsystem for Linux use
New-Item tests/__init__.pyinstead oftouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line
I make an empty file for the actual test
touch tests/test_attribute_error.pyAttention
on Windows without Windows Subsystem for Linux use
New-Item tests/test_attribute_error.pyinstead oftouch tests/test_attribute_error.pyNew-Item tests/test_attribute_error.pythe terminal goes back to the command line
I open
test_attribute_error.pyin the editor of the Integrated Development Environment (IDE)Tip
I can open a file from the terminal in Visual Studio Code by typing
codeand the name of the file, for example, when I type thiscode tests/test_attribute_error.pytest_attribute_error.pyopens up in the editorI add the first failing test to
test_attribute_error.py1import unittest 2 3 4class TestAttributeError(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True)I make a virtual environment in the terminal
python3 -m venv .venvAttention
on Windows without Windows Subsystem for Linux use
python3 -m venv .venvinstead ofpython3 -m venv .venvpython -m venv .venvthe terminal takes some time then goes back to the command line
I activate the virtual environment
source .venv/bin/activateAttention
on Windows without Windows Subsystem for Linux use
.venv/bin/activate.ps1NOTsource .venv/bin/activate.venv/scripts/activate.ps1the terminal shows
(.venv) .../pumping_python/attribute_errorI upgrade the Python package manager (pip) to the latest version
python3 -m pip install --upgrade pipthe terminal shows pip being uninstalled then installs the latest version or shows that it is already the latest version
I make a
requirements.txtfile for the Python programs my project needsecho "pytest-watch" > requirements.txtthe terminal goes back to the command line
I use pip to install
pytest-watchwith the requirements filepython3 -m pip install --requirement requirements.txtAttention
on Windows without Windows Subsystem for Linux use
python -m pip install --requirement requirements.txtinstead ofpython3 -m pip install --requirement requirements.txtpython -m pip install --requirement requirements.txtthe terminal shows pip downloads and installs the Python programs that pytest-watch needs to run
I use pytest-watch to run the test
pytest-watchthe terminal shows
================================ FAILURES ================================ _______________________ TestAttributeError.test_failure _______________________ self = <tests.test_attribute_error.AttributeError testMethod=test_failure> def test_failure(self): > self.assertFalse(True) E AssertionError: True is not false tests/test_attribute_error.py:7: AssertionError ========================= short test summary info ========================== FAILED tests/test_functions.py::TestFunctions::test_failure - AssertionError: True is not false =========================== 1 failed in X.YZs ============================I hold ctrl (Windows/Linux) or option/command (MacOS) on the keyboard and use the mouse to click on
tests/test_attribute_error.py:7to open it in the editorI add AssertionError to the list of Exceptions seen in
test_functions.py7 self.assertFalse(True) 8 9 10# Exceptions seen 11# AssertionErrorthen I change True to False in the assertion
7 self.assertFalse(False)the test passes
test_attribute_error_w_variables
RED: make it fail
I add an import statement at the top of
test_attribute_error.py1import src.attribute_error 2import unittestI change
test_failuretotest_attribute_error_w_variables5class TestAttributeError(unittest.TestCase): 6 7 def test_attribute_error_w_variables(self): 8 src.attribute_error.variable_00 9 10 11# Exceptions seenI think of
src.attribute_error.variable_00as an addresssrcis thesrcfolder.attribute_errorpoints toattribute_error.pyin thesrcfolder.variable_00points tovariable_00inattribute_error.pywhich is in thesrcfoldersrc.attribute_error.variable_00is pointing tovariable_00inattribute_error.pyin thesrcfolder
Since
attribute_error.pyis empty, Python cannot findvariable_00inside it and the terminal shows AttributeErrorAttributeError: module 'src.attribute_error' has no attribute 'variable_00'I add AttributeError to the list of Exceptions seen in
test_attribute_error.py11# Exceptions seen 12# AssertionError 13# AttributeError
GREEN: make it pass
I open
attribute_error.pyfrom thesrcfolder in the editor of my Integrated Development Environment (IDE), then I add a name1variable_00NameError: name 'variable_00' is not definedNameError is raised when I use a name that is not defined in the file I am working in
I add NameError to the list of Exceptions seen in
test_attribute_error.py11# Exceptions seen 12# AssertionError 13# AttributeError 14# NameErrorI point
variable_00to None inattribute_error.py1variable_00 = Nonethe test passes.
variable_00is now an attribute or property ofattribute_error.pywhich is in thesrcfolder and I can get to it withsrc.attribute_error.variable_00
REFACTOR: make it better
I do the same test a few more times as a drill in
test_attribute_error.py7 def test_attribute_error_w_variables(self): 8 src.attribute_error.variable_00 9 src.attribute_error.variable_01the terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_01'. Did you mean: 'variable_00'?I add the name to
attribute_error.py1variable_00 = None 2variable_01NameError: name 'variable_01' is not definedI point it to None to define it
1variable_00 = None 2variable_01 = Nonethe test passes
I another statement to
test_attribute_error.py7 def test_attribute_error_w_variables(self): 8 src.attribute_error.variable_00 9 src.attribute_error.variable_01 10 src.attribute_error.variable_02the terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_02'. Did you mean: 'variable_00'?I add the name and point it to None
1variable_00 = None 2variable_01 = None 3variable_02 = Nonethe test passes
one more line in
test_attribute_error.py7 def test_attribute_error_w_variables(self): 8 src.attribute_error.variable_00 9 src.attribute_error.variable_01 10 src.attribute_error.variable_02 11 src.attribute_error.variable_03the terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_03'. Did you mean: 'variable_00'?I add the attribute to
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = Nonethe test passes
test_attribute_error_w_functions
RED: make it fail
I add a new test to test_attribute_error.py
11 src.attribute_error.variable_03
12
13 def test_attribute_error_w_functions(self):
14 src.attribute_error.function_00()
15
16
17# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_00'
GREEN: make it pass
I add the name and point it to None in
attribute_error.py4variable_03 = None 5 6 7function_00 = NoneTypeError: 'NoneType' object is not callableI add TypeError to the list of Exceptions seen in
test_attribute_error.py17# Exceptions seen 18# AssertionError 19# AttributeError 20# NameError 21# TypeErrorI change the variable to a function in
attribute_error.py4variable_03 = None 5 6 7def function_00(): 8 return Nonethe test passes.
function_00is now an attribute or property ofattribute_error.pyin thesrcfolder. I can call it withsrc.attribute_error.function_00()
REFACTOR: make it better
time to do it as a drill, I add another call in
test_attribute_error.py13 def test_attribute_error_w_functions(self): 14 src.attribute_error.function_00() 15 src.attribute_error.function_01()the terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_01'. Did you mean: 'function_00'?I add the function to
attribute_error.py7def function_00(): 8 return None 9 10 11def function_01(): 12 return Nonethe test passes
I add another line to
test_attribute_error.py13 def test_attribute_error_w_functions(self): 14 src.attribute_error.function_00() 15 src.attribute_error.function_01() 16 src.attribute_error.function_02()the terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_02'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py11def function_01(): 12 return None 13 14 15def function_02(): 16 return Nonethe test passes
I add another line in
test_attribute_error.py13 def test_attribute_error_w_functions(self): 14 src.attribute_error.function_00() 15 src.attribute_error.function_01() 16 src.attribute_error.function_02() 17 src.attribute_error.function_03() 18 19 20# Exceptions seenthe terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_03'. Did you mean: 'function_00'?I add the function to
attribute_error.py15def function_02(): 16 return None 17 18 19def function_03(): 20 return Nonethe test passes.
test_attribute_error_w_class_attributes
I know that variables and functions defined in a module are attributes. variables defined inside a class are also attributes.
RED: make it fail
I add a new test to test_attribute_error.py
17 src.attribute_error.function_03()
18
19 def test_attribute_error_w_class_attributes(self):
20 src.attribute_error.AClass.attribute_00
21
22
23# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'AClass'
GREEN: make it pass
I add a function to
attribute_error.py15def function_03(): 16 return None 17 18 19def AClass(): 20 return Nonethe terminal shows AttributeError
AttributeError: 'function' object has no attribute 'attribute_00'I add a variable inside the function
23def AClass(): 24 25 attribute_00 = None 26 return Nonethe terminal still shows the same Exception because I cannot get to a variable inside a function from outside the function
I use the class keyword instead of the def keyword to make
AClassa class23class AClass(): 24 25 attribute_00 = None 26 return Nonethe terminal shows SyntaxError
E return None E ^^^^^^^^^^^ E SyntaxError: 'return' outside functionI cannot use a return statement outside a function
I add SyntaxError to the list of Exceptions seen in
test_attribute_error.py23# Exceptions seen 24# AssertionError 25# AttributeError 26# NameError 27# TypeError 28# SyntaxErrorI remove the return statement from
AClassinattribute_error.pysince it is no longer a function23class AClass(): 24 25 attribute_00 = Nonethe test passes.
attribute_00is now an attribute of theAClassclass which is an attribute ofattribute_error.pyin which is in thesrcfolder and I can get to it withsrc.attribute_error.AClass.attribute_00
REFACTOR: make it better
I add another failing line for practice to
test_attribute_error.py19 def test_attribute_error_w_class_attributes(self): 20 src.attribute_error.AClass.attribute_00 21 src.attribute_error.AClass.attribute_01the terminal shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_01'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py23class AClass(): 24 25 attribute_00 = None 26 attribute_01 = Nonethe test passes
I add another line to
test_attribute_error.py19 def test_attribute_error_w_class_attributes(self): 20 src.attribute_error.AClass.attribute_00 21 src.attribute_error.AClass.attribute_01 22 src.attribute_error.AClass.attribute_02the terminal shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_02'. Did you mean: 'attribute_00'?I add the attribute to
AClassinattribute_error.py23class AClass(): 24 25 attribute_00 = None 26 attribute_01 = None 27 attribute_02 = Nonethe test passes
I add another line to
test_attribute_error.py19 def test_attribute_error_w_class_attributes(self): 20 src.attribute_error.AClass.attribute_00 21 src.attribute_error.AClass.attribute_01 22 src.attribute_error.AClass.attribute_02 23 src.attribute_error.AClass.attribute_03 24 25 26# Exceptions seenthe terminal shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_03'. Did you mean: 'attribute_00'?I add the name to
AClassinattribute_error.py23class AClass(): 24 25 attribute_00 = None 26 attribute_01 = None 27 attribute_02 = None 28 attribute_03 = Nonethe test passes
test_attribute_error_w_class_methods
I know that variables, functions and classes defined in a module are attributes.
I also know that variables defined inside a class are attributes. functions defined inside a class are also attributes, they are called methods
RED: make it fail
I add a new test to
test_attribute_error.py23 src.attribute_error.AClass.attribute_03 24 25 def test_attribute_error_w_class_methods(self): 26 src.attribute_error.AClass.method_00() 27 28 29# Exceptions seenthe terminal shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_00'
GREEN: make it pass
I add the name to
AClassand point it to None inattribute_error.py23class AClass(): 24 25 attribute_00 = None 26 attribute_01 = None 27 attribute_02 = None 28 attribute_03 = None 29 30 method_00 = NoneTypeError: 'NoneType' object is not callableI make it a method with the def keyword
28 attribute_03 = None 29 30 def method_00(): 31 return Nonethe test passes.
method_00is now an attribute ofAClasswhich is an attribute ofattribute_error.pywhich is in thesrcfolder and I can get to it withsrc.attribute_error.AClass.method_00()
REFACTOR: make it better
You know the “drill”, I add a new failing line to
test_attribute_error.py25 def test_attribute_error_w_class_methods(self): 26 src.attribute_error.AClass.method_00() 27 src.attribute_error.AClass.method_01()the terminal shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_01'. Did you mean: 'method_00'?I add a definition for it in
attribute_error.py30 def method_00(): 31 return None 32 33 def method_01(): 34 return Nonethe test passes
I add another failing line to
test_attribute_error.py25 def test_attribute_error_w_class_methods(self): 26 src.attribute_error.AClass.method_00() 27 src.attribute_error.AClass.method_01() 28 src.attribute_error.AClass.method_02()the terminal shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_02'. Did you mean: 'method_00'?I add the method to
AClassinattribute_error.py33 def method_01(): 34 return None 35 36 def method_02(): 37 return Nonethe test passes
I add the last line to
test_attribute_error.py25 def test_attribute_error_w_class_methods(self): 26 src.attribute_error.AClass.method_00() 27 src.attribute_error.AClass.method_01() 28 src.attribute_error.AClass.method_02() 29 src.attribute_error.AClass.method_03() 30 31 32# Exceptions seenthe terminal shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_03'. Did you mean: 'method_00'?I add the method to
AClassinattribute_error.py36 def method_02(): 37 return None 38 39 def method_03(): 40 return Nonethe test passes
A function in a class is called a method and is an attribute of the class
close the project
I close
attribute_error.pyandtest_attribute_error.pyin the editorsI click in the terminal and exit the tests with ctrl+c on the keyboard
I deactivate the virtual environment
deactivatethe terminal goes back to the command line,
(.venv)is no longer on the left side.../pumping_python/attribute_errorI 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 with
I also saw the following Exceptions
code from the chapter
what is next?
you know
Would you like to test how to pass values from tests to functions with assert methods?
rate pumping python
If this has been a 7 star experience for you, please leave a 5 star review. It helps other people get into the book too