what causes AttributeError?
So far, all the tests show that I get AttributeError when I use a name that is NOT in an object.
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
2import unittest
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 src.attribute_error.variable_04
13 src.attribute_error.variable_05
14 src.attribute_error.variable_06
15 src.attribute_error.variable_07
16 src.attribute_error.variable_08
17 src.attribute_error.variable_09
18
19 def test_attribute_error_w_functions(self):
20 src.attribute_error.function_00()
21 src.attribute_error.function_01()
22 src.attribute_error.function_02()
23 src.attribute_error.function_03()
24 src.attribute_error.function_04()
25 src.attribute_error.function_05()
26 src.attribute_error.function_06()
27 src.attribute_error.function_07()
28 src.attribute_error.function_08()
29 src.attribute_error.function_09()
30
31 def test_attribute_error_w_class_attributes(self):
32 src.attribute_error.AClass.attribute_00
33 src.attribute_error.AClass.attribute_01
34 src.attribute_error.AClass.attribute_02
35 src.attribute_error.AClass.attribute_03
36 src.attribute_error.AClass.attribute_04
37 src.attribute_error.AClass().attribute_05
38 src.attribute_error.AClass().attribute_06
39 src.attribute_error.AClass().attribute_07
40 src.attribute_error.AClass().attribute_08
41 src.attribute_error.AClass().attribute_09
42
43 def test_attribute_error_w_class_methods(self):
44 src.attribute_error.AClass.method_00()
45 src.attribute_error.AClass.method_01
46 src.attribute_error.AClass().method_02()
47 src.attribute_error.AClass().method_03
48 src.attribute_error.AClass().method_04()
49 src.attribute_error.AClass.method_05
50 src.attribute_error.AClass.method_06()
51 src.attribute_error.AClass.method_07
52 src.attribute_error.AClass().method_08()
53 src.attribute_error.AClass().method_09
54
55
56# Exceptions seen
57# AssertionError
58# AttributeError
59# NameError
60# TypeError
61# SyntaxError
start the project
I name this project
attribute_errorI open a terminal
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 source code
mkdir srcthe terminal goes back to the command line.
I use the mv program to change the name of
main.pytoattribute_error.pyand move it to thesrcfoldermv main.py src/attribute_error.pyMove-Item main.py src/attribute_error.pythe terminal goes back to the command line.
I 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 open
test_attribute_error.pyI 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 go back to the terminal to make a requirements file for the Python packages I need
echo "pytest-watcher" > requirements.txtthe terminal goes back to the command line.
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 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 use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
======================== 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_attribute_error.py::TestAttributeError::test_failure - AssertionError: True is not false =================== 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_.pyif you ran
echo "pytest-watcher" > requirements.txt, to addpytest-watcherto the requirements file
fix those errors and try to run
uv run pytest-watcher . --nowagainI add AssertionError to the list of Exceptions seen in
test_attribute_error.py4class TestAttributeError(unittest.TestCase): 5 6 def test_failure(self): 7 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 thesrcfoldersrc.attribute_errorpoints toattribute_error.pyin thesrcfoldersrc.attribute_error.variable_00points tovariable_00inattribute_error.pyin thesrcfoldersince there is nothing in
attribute_error.pynamedvariable_00, Python cannot findvariable_00insideattribute_error.pyand raises AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_00'variable_00is NOT an attribute ofattribute_error.pyin thesrcfolderI 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 of my Integrated Development Environment (IDE)I delete all the text in the file, then add the name to
attribute_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.py11# Exceptions seen 12# AssertionError 13# AttributeError 14# NameErrorI point
variable_00to None, inattribute_error.py1variable_00 = Nonethe test passes because
variable_00is now an attribute/property ofattribute_error.pyin thesrcfolderI can use it from outside the file with
src.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_01 10 11 12# 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 the name to
attribute_error.py1variable_00 = None 2variable_01the terminal is my friend, and shows NameError
NameError: name 'variable_01' is not definedI point it to None to define it
1variable_00 = None 2variable_01 = Nonethe test passes.
I add 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_02 11 12 13# 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 the name and point it to None in
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = Nonethe test passes.
I add a line for
src.attribute_error.variable_03intest_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_03 12 13 14# 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 the variable to
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = Nonethe test passes.
I add a line for
src.attribute_error.variable_04intest_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_03 12 src.attribute_error.variable_04 13 14 15# 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 the variable to
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = Nonethe test passes.
I add a line for
src.attribute_error.variable_05intest_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_03 12 src.attribute_error.variable_04 13 src.attribute_error.variable_05 14 15 16# 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 the variable to
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = Nonethe test passes.
I add a line for
src.attribute_error.variable_06totest_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_03 12 src.attribute_error.variable_04 13 src.attribute_error.variable_05 14 src.attribute_error.variable_06 15 16 17# 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 the variable to
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = None 7variable_06 = Nonethe test passes.
I add a line for
src.attribute_error.variable_07totest_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_03 12 src.attribute_error.variable_04 13 src.attribute_error.variable_05 14 src.attribute_error.variable_06 15 src.attribute_error.variable_07 16 17 18# 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 the variable to
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = None 7variable_06 = None 8variable_07 = Nonethe test passes.
I add a line for
src.attribute_error.variable_08totest_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_03 12 src.attribute_error.variable_04 13 src.attribute_error.variable_05 14 src.attribute_error.variable_06 15 src.attribute_error.variable_07 16 src.attribute_error.variable_08 17 18 19# 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 the variable to
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = None 7variable_06 = None 8variable_07 = None 9variable_08 = Nonethe test passes.
I add a line for
src.attribute_error.variable_09totest_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_03 12 src.attribute_error.variable_04 13 src.attribute_error.variable_05 14 src.attribute_error.variable_06 15 src.attribute_error.variable_07 16 src.attribute_error.variable_08 17 src.attribute_error.variable_09 18 19 20# 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 the variable to
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = None 7variable_06 = None 8variable_07 = None 9variable_08 = None 10variable_09 = Nonethe test passes.
I open a new terminal then change directories to
assertion_errorcd assertion_errorthe terminal shows I am in the
assertion_errorfolder.../pumping_python/assertion_errorI add a git commit message
git commit -am \ 'add test_what_is_an_assertion'
test_attribute_error_w_functions
RED: make it fail
I add a test for functions to test_attribute_error.py
15 src.attribute_error.variable_07
16 src.attribute_error.variable_08
17 src.attribute_error.variable_09
18
19 def test_attribute_error_w_functions(self):
20 src.attribute_error.function_00()
21
22
23# Exceptions seen
the terminal is my friend, and 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.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = None 7variable_06 = None 8variable_07 = None 9variable_08 = None 10variable_09 = None 11 12 13function_00 = Nonethe 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.py23# Exceptions seen 24# AssertionError 25# AttributeError 26# NameError 27# TypeErrorI change the variable to a function in
attribute_error.py13# function_00 = None 14def function_00(): 15 return Nonethe test passes because
function_00is now an attribute/property ofattribute_error.pyin thesrcfolderI can call it from outside the file with
src.attribute_error.function_00()
REFACTOR: make it better
I remove the commented line
1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = None 7variable_06 = None 8variable_07 = None 9variable_08 = None 10variable_09 = None 11 12 13def function_00(): 14 return Nonetime to make it a drill.
I add a call to
src.attribute_error.function_01in test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(self): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 23 24# 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 the function to
attribute_error.py13def function_00(): 14 return None 15 16 17def function_01(): 18 return Nonethe test passes.
I add a line for
src.attribute_error.function_02to test_attribute_error_w_functions intest_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 18 19# 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 function for it in
attribute_error.py17def function_01(): 18 return None 19 20 21def function_02(): 22 return Nonethe test passes.
I add a line for
src.attribute_error.function_03to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(self): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 25 26# 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 function for it in
attribute_error.py21def function_02(): 22 return None 23 24 25def function_03(): 26 return Nonethe test passes.
I add a line for
src.attribute_error.function_04to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(self): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 26 27# 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 function for it in
attribute_error.py25def function_03(): 26 return None 27 28 29def function_04(): 30 return Nonethe test passes.
I add a line for
src.attribute_error.function_05to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(self): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 27 28# 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 function for it in
attribute_error.py29def function_04(): 30 return None 31 32 33def function_05(): 34 return Nonethe test passes.
I add a line for
src.attribute_error.function_06to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(self): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 src.attribute_error.function_06() 27 28 29# 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 function for it in
attribute_error.py33def function_05(): 34 return None 35 36 37def function_06(): 38 return Nonethe test passes.
I add a line for
src.attribute_error.function_07to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(self): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 src.attribute_error.function_06() 27 src.attribute_error.function_07() 28 29 30# 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 function for it in
attribute_error.py37def function_06(): 38 return None 39 40 41def function_07(): 42 return Nonethe test passes.
I add a line for
src.attribute_error.function_08to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(self): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 src.attribute_error.function_06() 27 src.attribute_error.function_07() 28 src.attribute_error.function_08() 29 30 31# 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 function for it in
attribute_error.py41def function_07(): 42 return None 43 44 45def function_08(): 46 return Nonethe test passes.
I add a line for
src.attribute_error.function_09to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(self): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 src.attribute_error.function_06() 27 src.attribute_error.function_07() 28 src.attribute_error.function_08() 29 src.attribute_error.function_09() 30 31 32# 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 function for it in
attribute_error.py45def function_08(): 46 return None 47 48 49def function_09(): 50 return Nonethe test passes.
I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_functions'
test_attribute_error_w_class_attributes
The tests show that variables and functions in a module are attributes of the module.
Is a class in a module also an attribute of the module?
RED: make it fail
I go back to the terminal that is running the tests
I add a test for class attributes to test_attribute_error.py
26 src.attribute_error.function_06()
27 src.attribute_error.function_07()
28 src.attribute_error.function_08()
29 src.attribute_error.function_09()
30
31 def test_attribute_error_w_class_attributes(self):
32 src.attribute_error.AClass.attribute_00
33
34
35# Exceptions seen
the terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error'
has no attribute 'AClass'
GREEN: make it pass
I add a function to
attribute_error.py49def function_09(): 50 return None 51 52 53def AClass(): 54 return Nonethe terminal is my friend, and shows AttributeError
AttributeError: 'function' object has no attribute 'attribute_00'I add a variable to the function
53def AClass(): 54 55 attribute_00 = None 56 return Nonethe terminal still shows the same Exception because I cannot get to a variable inside a function from outside the function. The variable is only used within the function when it runs.
I use the class keyword to change
AClassfrom a function to a class53# def AClass(): 54class AClass(object): 55 56 attribute_00 = None 57 return Nonethe terminal is my friend, and shows SyntaxError
E return None E ^^^^^^^^^^^ E SyntaxError: 'return' outside functionbecause I cannot use a return statement outside a function.
I add SyntaxError to the list of Exceptions seen in
test_attribute_error.py35# Exceptions seen 36# AssertionError 37# AttributeError 38# NameError 39# TypeError 40# SyntaxErrorI comment out the return statement from
AClassinattribute_error.pysince it is no longer a function53# def AClass(): 54class AClass(object): 55 56 attribute_00 = None 57 # return None
REFACTOR: make it better
I remove the commented lines
49def function_09(): 50 return None 51 52 53class AClass(object): 54 55 attribute_00 = NoneI add a line for
src.attribute_error.AClass.attribute_01totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 35 36# Exceptions seenthe terminal is my friend, and 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.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = Nonethe test passes.
I add a line for
src.attribute_error.AClass.attribute_02totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 src.attribute_error.AClass.attribute_02 35 36 37# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_02'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = Nonethe test passes.
I add a line for
src.attribute_error.AClass.attribute_03totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 src.attribute_error.AClass.attribute_02 35 src.attribute_error.AClass.attribute_03 36 37 38# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_03'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = Nonethe test passes.
I add a line for
src.attribute_error.AClass.attribute_04totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 src.attribute_error.AClass.attribute_02 35 src.attribute_error.AClass.attribute_03 36 src.attribute_error.AClass.attribute_04 37 38 39# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_04'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = Nonethe test passes.
I add a line for
src.attribute_error.AClass().attribute_05totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 src.attribute_error.AClass.attribute_02 35 src.attribute_error.AClass.attribute_03 36 src.attribute_error.AClass.attribute_04 37 src.attribute_error.AClass().attribute_05 38 39 40# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_05'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = Nonethe test passes because in this case it does not matter if I use the class (
AClass) or an instance of the class (AClass()).I add a line for
src.attribute_error.AClass().attribute_06totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 src.attribute_error.AClass.attribute_02 35 src.attribute_error.AClass.attribute_03 36 src.attribute_error.AClass.attribute_04 37 src.attribute_error.AClass().attribute_05 38 src.attribute_error.AClass().attribute_06 39 40 41# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_06'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = Nonethe test passes.
I add a line for
src.attribute_error.AClass().attribute_07totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 src.attribute_error.AClass.attribute_02 35 src.attribute_error.AClass.attribute_03 36 src.attribute_error.AClass.attribute_04 37 src.attribute_error.AClass().attribute_05 38 src.attribute_error.AClass().attribute_06 39 src.attribute_error.AClass().attribute_07 40 41 42# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_07'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = None 62 attribute_07 = Nonethe test passes.
I add a line for
src.attribute_error.AClass().attribute_08totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 src.attribute_error.AClass.attribute_02 35 src.attribute_error.AClass.attribute_03 36 src.attribute_error.AClass.attribute_04 37 src.attribute_error.AClass().attribute_05 38 src.attribute_error.AClass().attribute_06 39 src.attribute_error.AClass().attribute_07 40 src.attribute_error.AClass().attribute_08 41 42 43# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_08'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = None 62 attribute_07 = None 63 attribute_08 = Nonethe test passes.
I add a line for
src.attribute_error.AClass().attribute_09totest_attribute_error.py31 def test_attribute_error_w_class_attributes(self): 32 src.attribute_error.AClass.attribute_00 33 src.attribute_error.AClass.attribute_01 34 src.attribute_error.AClass.attribute_02 35 src.attribute_error.AClass.attribute_03 36 src.attribute_error.AClass.attribute_04 37 src.attribute_error.AClass().attribute_05 38 src.attribute_error.AClass().attribute_06 39 src.attribute_error.AClass().attribute_07 40 src.attribute_error.AClass().attribute_08 41 src.attribute_error.AClass().attribute_09 42 43 44# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'attribute_09'. Did you mean: 'attribute_00'?I add the name to the class definition in
attribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = None 62 attribute_07 = None 63 attribute_08 = None 64 attribute_09 = Nonethe test passes.
I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_class_attributes'
test_attribute_error_w_class_methods
The tests show that variables, functions and classes in a module are attributes of the module, and variables in a class are attributes of the class.
Methods of a class are also attributes of the class.
RED: make it fail
I go back to the terminal that is running the tests
I add a test for methods to
test_attribute_error.py38 src.attribute_error.AClass().attribute_06 39 src.attribute_error.AClass().attribute_07 40 src.attribute_error.AClass().attribute_08 41 src.attribute_error.AClass().attribute_09 42 43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 46 47# Exceptions seenthe terminal is my friend, and 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.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = None 62 attribute_07 = None 63 attribute_08 = None 64 attribute_09 = None 65 66 method_00 = Nonethe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause
method_00points to None and I cannot call None like a function.I use the def keyword to change it from a variable (attribute) to a method
53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = None 62 attribute_07 = None 63 attribute_08 = None 64 attribute_09 = None 65 66 # method_00 = None 67 def method_00(): 68 return None
REFACTOR: make it better
I remove the commented line
53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = None 62 attribute_07 = None 63 attribute_08 = None 64 attribute_09 = None 65 66 def method_00(): 67 return NoneYou know the “drill”, I add a line for
src.attribute_error.AClass.method_01totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 47 48# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_01'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = None 62 attribute_07 = None 63 attribute_08 = None 64 attribute_09 = None 65 66 def method_00(): 67 return None 68 69 def method_01(): 70 return Nonethe test passes because in this case it does not matter if I reference the method (
AClass.method_01) or call it (AClass.method_01()).I add a line for
src.attribute_error.AClass.method_02totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 src.attribute_error.AClass().method_02() 47 48 49# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_02'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py69 def method_01(): 70 return None 71 72 def method_02(): 73 return Nonethe terminal is my friend, and shows TypeError
TypeError: AClass.method_02() takes 0 positional arguments but 1 was givenbecause this happens when
AClass().method_02()is calledAClass().method_02() AClass.method_02(AClass)which raises TypeError since the definition of
method_02does not allow it take any positional arguments (the parentheses are empty).I add
selfto the parentheses ofmethod_0272 # def method_02(): 73 def method_02(self): 74 return Nonethe test passes because this happens when
AClass().method_02()is calledAClass().method_02() AClass.method_02(self)where
selfisAClass.I add the staticmethod decorator method definition instead of
selfsince it does not use anything in the class. That way I do not send more information than what the method needs.69 def method_01(): 70 return None 71 72 @staticmethod 73 def method_02(): 74 # def method_02(self): 75 return Nonethe test is still green because this now happens when
AClass().method_02()is calledAClass().method_02() AClass.method_02()with the staticmethod decorator it does not matter if I call the method from an instance (
AClass()) or from the class (AClass).I remove the commented line
69 def method_01(): 70 return None 71 72 @staticmethod 73 def method_02(): 74 return NoneI add a line for
src.attribute_error.AClass.method_03totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 src.attribute_error.AClass().method_02() 47 src.attribute_error.AClass().method_03 48 49 50# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_03'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py72 @staticmethod 73 def method_02(): 74 return None 75 76 def method_03(): 77 return Nonethe test passes because in this case I reference the method (
AClass().method_03), I do not call it (AClass().method_03()).I add a line for
src.attribute_error.AClass.method_04totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 src.attribute_error.AClass().method_02() 47 src.attribute_error.AClass().method_03 48 src.attribute_error.AClass().method_04() 49 50 51# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_04'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py76 def method_03(): 77 return None 78 79 def method_04(): 80 return Nonethe terminal is my friend, and shows TypeError
TypeError: AClass.method_04() takes 0 positional arguments but 1 was givenbecause this happens when
AClass().method_04()is calledAClass().method_04() AClass.method_04(AClass)which raises TypeError since the definition of
method_04does not allow it take any positional arguments.I add the staticmethod decorator to the definition for
method_0476 def method_03(): 77 return None 78 79 @staticmethod 80 def method_04(): 81 return Nonethe test passes because this now happens when
AClass().method_04()is calledAClass().method_04() AClass.method_04()with the staticmethod decorator it does not matter if I call the method from an instance (
AClass()) or from the class (AClass).I add a line for
src.attribute_error.AClass.method_05totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 src.attribute_error.AClass().method_02() 47 src.attribute_error.AClass().method_03 48 src.attribute_error.AClass().method_04() 49 src.attribute_error.AClass.method_05 50 51 52# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_05'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py79 @staticmethod 80 def method_04(): 81 return None 82 83 def method_05(): 84 return Nonethe test passes.
I add a line for
src.attribute_error.AClass.method_06totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 src.attribute_error.AClass().method_02() 47 src.attribute_error.AClass().method_03 48 src.attribute_error.AClass().method_04() 49 src.attribute_error.AClass.method_05 50 src.attribute_error.AClass.method_06() 51 52 53# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_06'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py83 def method_05(): 84 return None 85 86 def method_06(): 87 return Nonethe test passes because this happens when
AClass.method_06()is calledAClass.method_06()I called the method with the class (
AClass.method_06()) not an instance of the class (AClass().method_06()).I add a line for
src.attribute_error.AClass.method_07totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 src.attribute_error.AClass().method_02() 47 src.attribute_error.AClass().method_03 48 src.attribute_error.AClass().method_04() 49 src.attribute_error.AClass.method_05 50 src.attribute_error.AClass.method_06() 51 src.attribute_error.AClass.method_07 52 53 54# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_07'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py86 def method_06(): 87 return None 88 89 def method_07(): 90 return Nonethe test passes.
I add a line for
src.attribute_error.AClass.method_08totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 src.attribute_error.AClass().method_02() 47 src.attribute_error.AClass().method_03 48 src.attribute_error.AClass().method_04() 49 src.attribute_error.AClass.method_05 50 src.attribute_error.AClass.method_06() 51 src.attribute_error.AClass.method_07 52 src.attribute_error.AClass().method_08() 53 54 55# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_08'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py89 def method_07(): 90 return None 91 92 def method_08(): 93 return Nonethe terminal is my friend, and shows TypeError
TypeError: AClass.method_08() takes 0 positional arguments but 1 was givenbecause this happens when
AClass().method_08()is calledAClass().method_08() AClass.method_08(AClass)which raises TypeError since the definition of
method_08does not allow it take any positional arguments (the parentheses are empty).I add the staticmethod decorator to the method definition since it does not use anything in the class
89 def method_07(): 90 return None 91 92 @staticmethod 93 def method_08(): 94 return Nonethe test passes because this now happens when
AClass().method_08()is calledAClass().method_08() AClass.method_08()with the staticmethod decorator it does not matter if I call the method from an instance (
AClass()) or from the class (AClass).I add a line for
src.attribute_error.AClass.method_09totest_attribute_error.py43 def test_attribute_error_w_class_methods(self): 44 src.attribute_error.AClass.method_00() 45 src.attribute_error.AClass.method_01 46 src.attribute_error.AClass().method_02() 47 src.attribute_error.AClass().method_03 48 src.attribute_error.AClass().method_04() 49 src.attribute_error.AClass.method_05 50 src.attribute_error.AClass.method_06() 51 src.attribute_error.AClass.method_07 52 src.attribute_error.AClass().method_08() 53 src.attribute_error.AClass().method_09 54 55 56# Exceptions seen 57# AssertionError 58# AttributeError 59# NameError 60# TypeError 61# SyntaxErrorthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AClass' has no attribute 'method_09'. Did you mean: 'method_00'?I add the method to the definition of
AClassinattribute_error.py53class AClass(object): 54 55 attribute_00 = None 56 attribute_01 = None 57 attribute_02 = None 58 attribute_03 = None 59 attribute_04 = None 60 attribute_05 = None 61 attribute_06 = None 62 attribute_07 = None 63 attribute_08 = None 64 attribute_09 = None 65 66 def method_00(): 67 return None 68 69 def method_01(): 70 return None 71 72 @staticmethod 73 def method_02(): 74 return None 75 76 def method_03(): 77 return None 78 79 @staticmethod 80 def method_04(): 81 return None 82 83 def method_05(): 84 return None 85 86 def method_06(): 87 return None 88 89 def method_07(): 90 return None 91 92 @staticmethod 93 def method_08(): 94 return None 95 96 def method_09(): 97 return Nonethe test passes because in this case I reference the method (
AClass().method_09), I do not call it (AClass().method_09()).I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_class_methods'A function in a class is an attribute of the class and is called a method
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 with
I also saw these Exceptions
code from the chapter
what is next?
I know
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.