AttributeError with objects
test_attribute_error_w_variables and test_attribute_error_w_functions show that variables and functions in a module are attributes of the module.
Is an object in a module also an attribute of the module?
preview
I have these tests by the end of the chapter
open the project
I open a terminal
I change directory to the attribute_error folder in the
pumping_pythonfoldercd attribute_errorI open
test_attribute_error.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows
tests/test_attribute_error.py .. [100%] ================== 2 passed in D.EFs ===================
test_attribute_error_w_object_attributes
RED: make it fail
I go back to the terminal where the tests are running
I add a test for object attributes to
tests/test_attribute_error.py17def test_attribute_error_w_functions(): 18 src.attribute_error.function_00() 19 src.attribute_error.function_01() 20 src.attribute_error.function_02() 21 src.attribute_error.function_03() 22 src.attribute_error.function_04() 23 src.attribute_error.function_05() 24 src.attribute_error.function_06() 25 src.attribute_error.function_07() 26 src.attribute_error.function_08() 27 src.attribute_error.function_09() 28 29 30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 33 34# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'AnObject'
GREEN: make it pass
I open
__init__.pyfrom theattribute_errorfolder in thesrcfolderI add a function to
src/attribute_error/__init__.py22def function_09(): return function_08() 23 24 25def AnObject(): return function_09()the terminal is my friend, and shows AttributeError
AttributeError: 'function' object has no attribute 'attribute_00'I add a variable for
attribute_00to the function25# def AnObject(): return function_09() 26def AnObject(): 27 28 attribute_00 = function_09() 29 return function_09()the 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 inside the function when it runs.
I change
AnObjectfrom a function to an object25# def AnObject(): return function_09() 26# def AnObject(): 27class AnObject(object): 28 29 attribute_00 = function_09() 30 return function_09()the terminal is my friend, and shows SyntaxError
E return function_09() 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
tests/test_attribute_error.py34# Exceptions seen 35# AssertionError 36# AttributeError 37# NameError 38# TypeError 39# SyntaxErrorI comment out the return statement from
AnObjectinsrc/attribute_error/__init__.py25# def AnObject(): return function_09() 26# def AnObject(): 27class AnObject(object): 28 29 attribute_00 = function_09() 30 # return function_09()the test passes because
attribute_00is now an attribute/property of theAnObjectobjectAnObjectis an attribute of theattribute_errormodule in thesrcfolderI can use
attribute_00from outside the file withsrc.attribute_error.AnObject.attribute_00orsrc.attribute_error.AnObject().attribute_00src.attribute_error.AnObject.attribute_00 src.attribute_error.AnObject().attribute_00 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_00 = function_09()
REFACTOR: make it better
I remove the commented lines
22def function_09(): return function_08() 23 24 25class AnObject(object): 26 27 attribute_00 = function_09()I add a line for
src.attribute_error.AnObject().attribute_01totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 34 35# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'attribute_01'. Did you mean: 'attribute_00'?I add
attribute_01to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00the test passes because in this case it does not matter if I use the object (
AnObject) or an instance of the class (AnObject()).attribute_01is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_01orsrc.attribute_error.AnObject().attribute_01src.attribute_error.AnObject.attribute_01 src.attribute_error.AnObject().attribute_01 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_01 = attribute_00
I add a line for
src.attribute_error.AnObject.attribute_02totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 src.attribute_error.AnObject.attribute_02 34 35 36# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'attribute_02'. Did you mean: 'attribute_00'?I add
attribute_02to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01the test passes because
attribute_02is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_02orsrc.attribute_error.AnObject().attribute_02src.attribute_error.AnObject.attribute_02 src.attribute_error.AnObject().attribute_02 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_02 = attribute_01I add a line for
src.attribute_error.AnObject().attribute_03totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 src.attribute_error.AnObject.attribute_02 34 src.attribute_error.AnObject().attribute_03 35 36 37# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'attribute_03'. Did you mean: 'attribute_00'?I add
attribute_03to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02the test passes because
attribute_03is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_03orsrc.attribute_error.AnObject().attribute_03src.attribute_error.AnObject.attribute_03 src.attribute_error.AnObject().attribute_03 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_03 = attribute_02I add a line for
src.attribute_error.AnObject.attribute_04totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 src.attribute_error.AnObject.attribute_02 34 src.attribute_error.AnObject().attribute_03 35 src.attribute_error.AnObject.attribute_04 36 37 38# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'attribute_04'. Did you mean: 'attribute_00'?I add
attribute_04to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03the test passes because
attribute_04is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_04orsrc.attribute_error.AnObject().attribute_04src.attribute_error.AnObject.attribute_04 src.attribute_error.AnObject().attribute_04 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_04 = attribute_03I add a line for
src.attribute_error.AnObject().attribute_05totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 src.attribute_error.AnObject.attribute_02 34 src.attribute_error.AnObject().attribute_03 35 src.attribute_error.AnObject.attribute_04 36 src.attribute_error.AnObject().attribute_05 37 38 39# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'attribute_05'. Did you mean: 'attribute_00'?I add
attribute_05to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03 32 attribute_05 = attribute_04the test passes because
attribute_05is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_05orsrc.attribute_error.AnObject().attribute_05src.attribute_error.AnObject.attribute_05 src.attribute_error.AnObject().attribute_05 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_05 = attribute_04I add a line for
src.attribute_error.AnObject().attribute_06totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 src.attribute_error.AnObject.attribute_02 34 src.attribute_error.AnObject().attribute_03 35 src.attribute_error.AnObject.attribute_04 36 src.attribute_error.AnObject().attribute_05 37 src.attribute_error.AnObject().attribute_06 38 39 40# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'attribute_06'. Did you mean: 'attribute_00'?I add
attribute_06to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03 32 attribute_05 = attribute_04 33 attribute_06 = attribute_05the test passes because
attribute_06is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_06orsrc.attribute_error.AnObject().attribute_06src.attribute_error.AnObject.attribute_06 src.attribute_error.AnObject().attribute_06 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_06 = attribute_05I add a line for
src.attribute_error.AnObject.attribute_07totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 src.attribute_error.AnObject.attribute_02 34 src.attribute_error.AnObject().attribute_03 35 src.attribute_error.AnObject.attribute_04 36 src.attribute_error.AnObject().attribute_05 37 src.attribute_error.AnObject().attribute_06 38 src.attribute_error.AnObject.attribute_07 39 40 41# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'attribute_07'. Did you mean: 'attribute_00'?I add
attribute_07to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03 32 attribute_05 = attribute_04 33 attribute_06 = attribute_05 34 attribute_07 = attribute_06the test passes because
attribute_07is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_07orsrc.attribute_error.AnObject().attribute_07src.attribute_error.AnObject.attribute_07 src.attribute_error.AnObject().attribute_07 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_07 = attribute_06I add a line for
src.attribute_error.AnObject.attribute_08totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 src.attribute_error.AnObject.attribute_02 34 src.attribute_error.AnObject().attribute_03 35 src.attribute_error.AnObject.attribute_04 36 src.attribute_error.AnObject().attribute_05 37 src.attribute_error.AnObject().attribute_06 38 src.attribute_error.AnObject.attribute_07 39 src.attribute_error.AnObject.attribute_08 40 41 42# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'attribute_08'. Did you mean: 'attribute_00'?I add
attribute_08to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03 32 attribute_05 = attribute_04 33 attribute_06 = attribute_05 34 attribute_07 = attribute_06 35 attribute_08 = attribute_07the test passes because
attribute_08is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_08orsrc.attribute_error.AnObject().attribute_08src.attribute_error.AnObject.attribute_08 src.attribute_error.AnObject().attribute_08 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_08 = attribute_07I add a line for
src.attribute_error.AnObject().attribute_09totests/test_attribute_error.py30def test_attribute_error_w_object_attributes(): 31 src.attribute_error.AnObject.attribute_00 32 src.attribute_error.AnObject().attribute_01 33 src.attribute_error.AnObject.attribute_02 34 src.attribute_error.AnObject().attribute_03 35 src.attribute_error.AnObject.attribute_04 36 src.attribute_error.AnObject().attribute_05 37 src.attribute_error.AnObject().attribute_06 38 src.attribute_error.AnObject.attribute_07 39 src.attribute_error.AnObject.attribute_08 40 src.attribute_error.AnObject().attribute_09 41 42 43# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'attribute_09'. Did you mean: 'attribute_00'?I add
attribute_09to the object definition insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03 32 attribute_05 = attribute_04 33 attribute_06 = attribute_05 34 attribute_07 = attribute_06 35 attribute_08 = attribute_07 36 attribute_09 = attribute_08the test passes because
attribute_09is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can use it from outside the file withsrc.attribute_error.AnObject.attribute_09orsrc.attribute_error.AnObject().attribute_09src.attribute_error.AnObject.attribute_09 src.attribute_error.AnObject().attribute_09 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── attribute_09 = attribute_08I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_object_attributes'
test_attribute_error_w_object_methods
The tests show that variables, functions and objects in a module are attributes of the module, and variables in an object 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 where the tests are running
I add a test for methods to
tests/test_attribute_error.py40 src.attribute_error.AnObject().attribute_09 41 42 43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject.method_00() 45 46 47# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'method_00'
GREEN: make it pass
I add the name to
AnObjectand point it toattribute_09, insrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03 32 attribute_05 = attribute_04 33 attribute_06 = attribute_05 34 attribute_07 = attribute_06 35 attribute_08 = attribute_07 36 attribute_09 = attribute_08 37 38 method_00 = attribute_09the terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause
method_00isattribute_09,attribute_09is None, and I cannot call None like a function.I use the def keyword to change it from a variable (attribute) to a method
36 attribute_09 = attribute_08 37 38 # method_00 = attribute_09 39 def method_00(): return attribute_09the terminal is my friend, and shows NameError
NameError: name 'attribute_09' is not definedbecause there is nothing inside
method_00namedattribute_09and it cannot get toattribute_09ofAnObject, yet.I add
self.before the name to reference the object attribute38 # method_00 = attribute_09 39 # def method_00(): return attribute_09 40 def method_00(): return self.attribute_09the terminal is my friend, and shows NameError
NameError: name 'self' is not definedI add
selfto the parentheses ofmethod_0038 # method_00 = attribute_09 39 # def method_00(): return attribute_09 40 # def method_00(): return self.attribute_09 41 def method_00(self): return self.attribute_09the terminal is my friend, and shows TypeError
TypeError: AnObject.method_00() missing 1 required positional argument: 'self'because I called the method with the object not an instance of the class.
I use an instance of the class to call the method in
tests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 # src.attribute_error.AnObject.method_00() 45 src.attribute_error.AnObject().method_00() 46 47 48# Exceptions seenthe test passes because
method_00is now an attribute/property of theAnObjectobjectAnObjectis an attribute of theattribute_errormodule in thesrcfolderI can call
method_00from outside the file withsrc.attribute_error.AnObject().method_00()
src.attribute_error.AnObject().method_00 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── def method_00(self): └── return self.attribute_09
REFACTOR: make it better
I remove the commented lines from
src/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03 32 attribute_05 = attribute_04 33 attribute_06 = attribute_05 34 attribute_07 = attribute_06 35 attribute_08 = attribute_07 36 attribute_09 = attribute_08 37 38 def method_00(self): return self.attribute_09I remove the commented line from
tests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 46 47# Exceptions seenYou know the “drill”, I add a call to
src.attribute_error.AnObject().method_0143def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 47 48# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_01'. Did you mean: 'method_00'?I add the method to the definition of
AnObjectinsrc/attribute_error/__init__.py38 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00()the test passes because
method_01is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can call it from outside the file withsrc.attribute_error.AnObject().method_01()src.attribute_error.AnObject().method_01 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── def method_01(self): └── return self.method_00()I add a call to
src.attribute_error.AnObject().method_02fromtests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 src.attribute_error.AnObject().method_02() 47 48 49# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_02'. Did you mean: 'method_00'?I add the method to the definition of
AnObjectinsrc/attribute_error/__init__.py38 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00() 40 def method_02(self): return self.method_01()the test passes because
method_02is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can call it from outside the file withsrc.attribute_error.AnObject().method_02()src.attribute_error.AnObject().method_02 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── def method_02(self): └── return self.method_01()I add a call to
src.attribute_error.AnObject().method_03fromtests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 src.attribute_error.AnObject().method_02() 47 src.attribute_error.AnObject().method_03() 48 49 50# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_03'. Did you mean: 'method_00'?I add a method definition for
method_03to the definition ofAnObjectinsrc/attribute_error/__init__.py38 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00() 40 def method_02(self): return self.method_01() 41 def method_03(self): return self.method_02()the test passes because
method_03is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can call it from outside the file withsrc.attribute_error.AnObject().method_03()src.attribute_error.AnObject().method_03 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── def method_03(self): └── return self.method_02()I add a call to
src.attribute_error.AnObject().method_04fromtests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 src.attribute_error.AnObject().method_02() 47 src.attribute_error.AnObject().method_03() 48 src.attribute_error.AnObject().method_04() 49 50 51# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_04'. Did you mean: 'method_00'?I add a method definition for
method_04to the definition ofAnObjectinsrc/attribute_error/__init__.py38 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00() 40 def method_02(self): return self.method_01() 41 def method_03(self): return self.method_02() 42 def method_04(self): return self.method_03()the test passes because
method_04is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can call it from outside the file withsrc.attribute_error.AnObject().method_04()src.attribute_error.AnObject().method_04 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── def method_04(self): return self.method_03()I add a call to
src.attribute_error.AnObject().method_05fromtests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 src.attribute_error.AnObject().method_02() 47 src.attribute_error.AnObject().method_03() 48 src.attribute_error.AnObject().method_04() 49 src.attribute_error.AnObject().method_05() 50 51 52# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_05'. Did you mean: 'method_00'?I add a method definition for
method_05to the definition ofAnObjectinsrc/attribute_error/__init__.py38 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00() 40 def method_02(self): return self.method_01() 41 def method_03(self): return self.method_02() 42 def method_04(self): return self.method_03() 43 def method_05(self): return self.method_04()the test passes.
I add a call to
src.attribute_error.AnObject().method_06fromtests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 src.attribute_error.AnObject().method_02() 47 src.attribute_error.AnObject().method_03() 48 src.attribute_error.AnObject().method_04() 49 src.attribute_error.AnObject().method_05() 50 src.attribute_error.AnObject().method_06() 51 52 53# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_06'. Did you mean: 'method_00'?I add a method definition for
method_06to the definition ofAnObjectinsrc/attribute_error/__init__.py38 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00() 40 def method_02(self): return self.method_01() 41 def method_03(self): return self.method_02() 42 def method_04(self): return self.method_03() 43 def method_05(self): return self.method_04() 44 def method_06(self): return self.method_05()the test passes.
I add a call to
src.attribute_error.AnObject().method_07fromtests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 src.attribute_error.AnObject().method_02() 47 src.attribute_error.AnObject().method_03() 48 src.attribute_error.AnObject().method_04() 49 src.attribute_error.AnObject().method_05() 50 src.attribute_error.AnObject().method_06() 51 src.attribute_error.AnObject().method_07() 52 53 54# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_07'. Did you mean: 'method_00'?I add a method definition for
method_07to the definition ofAnObjectinsrc/attribute_error/__init__.py38 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00() 40 def method_02(self): return self.method_01() 41 def method_03(self): return self.method_02() 42 def method_04(self): return self.method_03() 43 def method_05(self): return self.method_04() 44 def method_06(self): return self.method_05() 45 def method_07(self): return self.method_06()the test passes.
I add a call to
src.attribute_error.AnObject().method_08fromtests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 src.attribute_error.AnObject().method_02() 47 src.attribute_error.AnObject().method_03() 48 src.attribute_error.AnObject().method_04() 49 src.attribute_error.AnObject().method_05() 50 src.attribute_error.AnObject().method_06() 51 src.attribute_error.AnObject().method_07() 52 src.attribute_error.AnObject().method_08() 53 54 55# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_08'. Did you mean: 'method_00'?I add a method definition for
method_08to the definition ofAnObjectinsrc/attribute_error/__init__.py38 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00() 40 def method_02(self): return self.method_01() 41 def method_03(self): return self.method_02() 42 def method_04(self): return self.method_03() 43 def method_05(self): return self.method_04() 44 def method_06(self): return self.method_05() 45 def method_07(self): return self.method_06() 46 def method_08(self): return self.method_07()the test passes.
I add a call to
src.attribute_error.AnObject().method_09fromtests/test_attribute_error.py43def test_attribute_error_w_object_methods(): 44 src.attribute_error.AnObject().method_00() 45 src.attribute_error.AnObject().method_01() 46 src.attribute_error.AnObject().method_02() 47 src.attribute_error.AnObject().method_03() 48 src.attribute_error.AnObject().method_04() 49 src.attribute_error.AnObject().method_05() 50 src.attribute_error.AnObject().method_06() 51 src.attribute_error.AnObject().method_07() 52 src.attribute_error.AnObject().method_08() 53 src.attribute_error.AnObject().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: 'AnObject' object has no attribute 'method_09'. Did you mean: 'method_00'?I add a method definition for
method_09to the definition ofAnObjectinsrc/attribute_error/__init__.py25class AnObject(object): 26 27 attribute_00 = function_09() 28 attribute_01 = attribute_00 29 attribute_02 = attribute_01 30 attribute_03 = attribute_02 31 attribute_04 = attribute_03 32 attribute_05 = attribute_04 33 attribute_06 = attribute_05 34 attribute_07 = attribute_06 35 attribute_08 = attribute_07 36 attribute_09 = attribute_0838 def method_00(self): return self.attribute_09 39 def method_01(self): return self.method_00() 40 def method_02(self): return self.method_01() 41 def method_03(self): return self.method_02() 42 def method_04(self): return self.method_03() 43 def method_05(self): return self.method_04() 44 def method_06(self): return self.method_05() 45 def method_07(self): return self.method_06() 46 def method_08(self): return self.method_07() 47 def method_09(self): return self.method_08()the test passes because
method_09is now an attribute ofAnObjectinsrc/attribute_error/__init__.py, and I can call it from outside the file withsrc.attribute_error.AnObject().method_09()src.attribute_error.AnObject().method_09 └── src/ └── attribute_error/ └── __init__.py └── class AnObject(object): └── def method_09(self): └── return self.method_08()I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_object_methods'
A function in an object is an attribute of the class and is called a method
close the project
I close
src/attribute_error/__init__.pyandtests/test_attribute_error.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
attribute_errorcd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
All the tests I have run for AttributeError show that
I still have the problem that the tests all show the correct way to use attributes I made in src/attribute_error/__init__.py. If someone reads the file or runs it, there is no way for them to know how the code relates to AttributeError unless they go through the process with me, there has to be a better way.
code from the chapter
what is next?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.