AttributeError¶
AttributeError is raised when there is a reference to a name that is NOT in an object that exists.
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
requirements¶
I open a terminal to run makePythonTdd.sh with
attribute_erroras the name of the project./makePythonTdd.sh attribute_erroron Windows without Windows Subsystem Linux use makePythonTdd.ps1 instead of makePythonTdd.sh
./makePythonTdd.ps1 attribute_error
it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError
E AssertionError: True is not false tests/test_attribute_error.py:7: AssertionError
I hold
ctrl(windows/linux) oroption(mac) on the keyboard and use the mouse to click ontests/test_attribute_error.py:7to open it in the editorthen I change
TruetoFalseto make the test pass7 self.assertFalse(False)I change the name of the class to match the CapWords format
4class TestAttributeError(unittest.TestCase):
test_attribute_error_w_variables¶
red: make it fail¶
I add an import statement at the top of
test_attribute_error.py1import unittest 2import src.attribute_error
I 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 Encountered
I think of
src.attribute_error.variable_00as an address forvariable_00inattribute_error.pywhich is in thesrcfolder. Since the file is empty, the variable is not in it.The terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_00'
I add the error to the list of Exceptions encountered in
test_attribute_error.py11# Exceptions Encountered 12# AssertionError 13# AttributeError
green: make it pass¶
I click on
attribute_error.pyin thesrcfolder to open it in the editor of my Integrated Development Environment (IDE), then I add a name1variable_00NameError: name 'variable_00' is not defined
I add it to the list of Exceptions encountered in
test_attribute_error.py11# Exceptions Encountered 12# AssertionError 13# AttributeError 14# NameError
I point
variable_00to None inattribute_error.py1variable_00 = Nonethe test passes.
variable_00is now an attribute/property ofattribute_error.pywhich is in thesrcfolder and I can reach it by usingsrc.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
the 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_01
NameError: name 'variable_01' is not defined
I point it to None
1variable_00 = None 2variable_01 = None
the 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_02
the 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 = None
the 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_03
the 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 = None
the 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 Encountered
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 = None
TypeError: 'NoneType' object is not callable
I add it to the list of Exceptions encountered in
test_attribute_error.py17# Exceptions Encountered 18# AssertionError 19# AttributeError 20# NameError 21# TypeError
I change the attribute to a function in
attribute_error.py4variable_03 = None 5 6 7def function_00(): 8 return None
the test passes.
function_00is now an attribute/property ofattribute_error.pywhich is in thesrcfolder and I can call it by usingsrc.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 None
the 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 None
the test passes
then 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 Encountered
the terminal shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_03'. Did you mean: 'function_00'?
I add it the function to
attribute_error.py15def function_02(): 16 return None 17 18 19def function_03(): 20 return None
the test passes
test_attribute_error_w_class_attributes¶
We 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
src.attribute_error.function_03()
def test_attribute_error_w_class_attributes(self):
src.attribute_error.AClass.attribute_00
# Exceptions Encountered
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 None
the terminal shows AttributeError
AttributeError: 'function' object has no attribute 'attribute_00'
I define a variable inside the function
23def AClass(): 24 25 attribute_00 = None 26 return None
the terminal still shows the same Exception because I cannot access a variable that belongs to a function from outside of the function
I use the class keyword instead of the def keyword to make
AClassa class23class AClass(): 24 25 attribute_00 = None 26 return None
the terminal shows SyntaxError
E return None E ^^^^^^^^^^^ E SyntaxError: 'return' outside function
I add it to the list of Exceptions encountered in
test_attribute_error.py23# Exceptions Encountered 24# AssertionError 25# AttributeError 26# NameError 27# TypeError 28# SyntaxError
I remove the return statement from
AClassinattribute_error.pysince it is no longer a function23class AClass(): 24 attribute_00 = None
the test passes.
attribute_00is now an attribute/property ofAClasswhich is an attribute/property ofattribute_error.pywhich is in thesrcfolder and I can reach it by usingsrc.attribute_error.AClass.attribute_00()
refactor: make it better¶
I add another failing 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
the 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 = None
the 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
the 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 = None
the 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 Encountered
the 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 = None
the test passes
A variable in a class in a module is an attribute of the class.
test_attribute_error_w_class_methods¶
We know that variables, functions and classes defined in a module are attributes. We also know that variables defined inside a class are attributes.
functions defined inside a class are also attributes, they are known as 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 Encountered
the 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 = None
TypeError: 'NoneType' object is not callable
I make it a method by using the def keyword
28 attribute_03 = None 29 30 def method_00(): 31 return None
the test passes.
method_00is now an attribute/property ofAClasswhich is an attribute/property ofattribute_error.pywhich is in thesrcfolder and I can reach it by usingsrc.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 None
the terminal shows green again
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 None
the 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 Encountered
the 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 None
the test passes
A function in a class is called a method and is an attribute of the class
review¶
I ran tests for AttributeError with
I also ran into the following Exceptions
Click here to see the code I typed in this chapter