AttributeError with assertRaises
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 a class in a module also an attribute of the module?
preview
I have these tests by the end of the chapter
1import src.attribute_error
2
3
4def test_attribute_error_w_variables():
5 src.attribute_error.variable_00
6 src.attribute_error.variable_01
7 src.attribute_error.variable_02
8 src.attribute_error.variable_03
9 src.attribute_error.variable_04
10 src.attribute_error.variable_05
11 src.attribute_error.variable_06
12 src.attribute_error.variable_07
13 src.attribute_error.variable_08
14 src.attribute_error.variable_09
15
16
17def test_attribute_error_w_functions():
18 src.attribute_error.function_00()
19 src.attribute_error.function_01()
20 src.attribute_error.function_02()
21 src.attribute_error.function_03()
22 src.attribute_error.function_04()
23 src.attribute_error.function_05()
24 src.attribute_error.function_06()
25 src.attribute_error.function_07()
26 src.attribute_error.function_08()
27 src.attribute_error.function_09()
28
29
30def test_attribute_error_w_class_attributes():
31 src.attribute_error.AClass.attribute_00
32 src.attribute_error.AClass().attribute_01
33 src.attribute_error.AClass.attribute_02
34 src.attribute_error.AClass().attribute_03
35 src.attribute_error.AClass.attribute_04
36 src.attribute_error.AClass().attribute_05
37 src.attribute_error.AClass().attribute_06
38 src.attribute_error.AClass.attribute_07
39 src.attribute_error.AClass.attribute_08
40 src.attribute_error.AClass().attribute_09
41
42
43def test_attribute_error_w_class_methods():
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# ModuleNotFoundError
59# AttributeError
60# NameError
61# TypeError
62# SyntaxError
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.pyI 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%] ================== 7 passed in D.EFs ===================
test_attribute_error_w_class_attributes
RED: make it fail
I go back to the terminal where the tests are running
I add a test for class attributes to
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_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 33 34# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'AClass'
GREEN: make it pass
I open
attribute_error.pyI add a function to
attribute_error.py22def function_09(): return function_08() 23 24 25def AClass(): 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 AClass(): return function_09() 26def AClass(): 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
AClassfrom a function to a class25# def AClass(): return function_09() 26# def AClass(): 27class AClass(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
test_attribute_error.py34# Exceptions seen 35# AssertionError 36# ModuleNotFoundError 37# AttributeError 38# NameError 39# TypeError 40# SyntaxErrorI comment out the return statement from
AClassinattribute_error.py25# def AClass(): return function_09() 26# def AClass(): 27class AClass(object): 28 29 attribute_00 = function_09() 30 # return function_09()the test passes because
attribute_00is now an attribute/property of theAClassclassAClassis an attribute of theattribute_error.pymodule in thesrcfolderI can use
attribute_00from outside the file withsrc.attribute_error.AClass.attribute_00orsrc.attribute_error.AClass().attribute_00src.attribute_error.AClass.attribute_00 src.attribute_error.AClass().attribute_00 src └── attribute_error.py └── class AClass(object): └── attribute_00 = function_09()
REFACTOR: make it better
I remove the commented lines
22def function_09(): return function_08() 23 24 25class AClass(object): 26 27 attribute_00 = function_09()I add a line for
src.attribute_error.AClass().attribute_01totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 34 35# 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
attribute_01to the class definition inattribute_error.py25class AClass(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 class (
AClass) or an instance of the class (AClass()).attribute_01is now an attribute ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_01orsrc.attribute_error.AClass().attribute_01src.attribute_error.AClass.attribute_01 src.attribute_error.AClass().attribute_01 src └── attribute_error.py └── class AClass(object): └── attribute_01 = attribute_00
I add a line for
src.attribute_error.AClass.attribute_02totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 src.attribute_error.AClass.attribute_02 34 35 36# 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
attribute_02to the class definition inattribute_error.py25class AClass(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 ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_02orsrc.attribute_error.AClass().attribute_02src.attribute_error.AClass.attribute_02 src.attribute_error.AClass().attribute_02 src └── attribute_error.py └── class AClass(object): └── attribute_02 = attribute_01I add a line for
src.attribute_error.AClass().attribute_03totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 src.attribute_error.AClass.attribute_02 34 src.attribute_error.AClass().attribute_03 35 36 37# 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
attribute_03to the class definition inattribute_error.py25class AClass(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 ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_03orsrc.attribute_error.AClass().attribute_03src.attribute_error.AClass.attribute_03 src.attribute_error.AClass().attribute_03 src └── attribute_error.py └── class AClass(object): └── attribute_03 = attribute_02I add a line for
src.attribute_error.AClass.attribute_04totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 src.attribute_error.AClass.attribute_02 34 src.attribute_error.AClass().attribute_03 35 src.attribute_error.AClass.attribute_04 36 37 38# 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
attribute_04to the class definition inattribute_error.py25class AClass(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 ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_04orsrc.attribute_error.AClass().attribute_04src.attribute_error.AClass.attribute_04 src.attribute_error.AClass().attribute_04 src └── attribute_error.py └── class AClass(object): └── attribute_04 = attribute_03I add a line for
src.attribute_error.AClass().attribute_05totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 src.attribute_error.AClass.attribute_02 34 src.attribute_error.AClass().attribute_03 35 src.attribute_error.AClass.attribute_04 36 src.attribute_error.AClass().attribute_05 37 38 39# 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
attribute_05to the class definition inattribute_error.py25class AClass(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 ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_05orsrc.attribute_error.AClass().attribute_05src.attribute_error.AClass.attribute_05 src.attribute_error.AClass().attribute_05 src └── attribute_error.py └── class AClass(object): └── attribute_05 = attribute_04I add a line for
src.attribute_error.AClass().attribute_06totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 src.attribute_error.AClass.attribute_02 34 src.attribute_error.AClass().attribute_03 35 src.attribute_error.AClass.attribute_04 36 src.attribute_error.AClass().attribute_05 37 src.attribute_error.AClass().attribute_06 38 39 40# 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
attribute_06to the class definition inattribute_error.py25class AClass(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 ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_06orsrc.attribute_error.AClass().attribute_06src.attribute_error.AClass.attribute_06 src.attribute_error.AClass().attribute_06 src └── attribute_error.py └── class AClass(object): └── attribute_06 = attribute_05I add a line for
src.attribute_error.AClass.attribute_07totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 src.attribute_error.AClass.attribute_02 34 src.attribute_error.AClass().attribute_03 35 src.attribute_error.AClass.attribute_04 36 src.attribute_error.AClass().attribute_05 37 src.attribute_error.AClass().attribute_06 38 src.attribute_error.AClass.attribute_07 39 40 41# 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
attribute_07to the class definition inattribute_error.py25class AClass(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 ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_07orsrc.attribute_error.AClass().attribute_07src.attribute_error.AClass.attribute_07 src.attribute_error.AClass().attribute_07 src └── attribute_error.py └── class AClass(object): └── attribute_07 = attribute_06I add a line for
src.attribute_error.AClass.attribute_08totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 src.attribute_error.AClass.attribute_02 34 src.attribute_error.AClass().attribute_03 35 src.attribute_error.AClass.attribute_04 36 src.attribute_error.AClass().attribute_05 37 src.attribute_error.AClass().attribute_06 38 src.attribute_error.AClass.attribute_07 39 src.attribute_error.AClass.attribute_08 40 41 42# 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
attribute_08to the class definition inattribute_error.py25class AClass(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 ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_08orsrc.attribute_error.AClass().attribute_08src.attribute_error.AClass.attribute_08 src.attribute_error.AClass().attribute_08 src └── attribute_error.py └── class AClass(object): └── attribute_08 = attribute_07I add a line for
src.attribute_error.AClass().attribute_09totest_attribute_error.py30def test_attribute_error_w_class_attributes(): 31 src.attribute_error.AClass.attribute_00 32 src.attribute_error.AClass().attribute_01 33 src.attribute_error.AClass.attribute_02 34 src.attribute_error.AClass().attribute_03 35 src.attribute_error.AClass.attribute_04 36 src.attribute_error.AClass().attribute_05 37 src.attribute_error.AClass().attribute_06 38 src.attribute_error.AClass.attribute_07 39 src.attribute_error.AClass.attribute_08 40 src.attribute_error.AClass().attribute_09 41 42 43# 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
attribute_09to the class definition inattribute_error.py25class AClass(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 ofAClassinattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.AClass.attribute_09orsrc.attribute_error.AClass().attribute_09src.attribute_error.AClass.attribute_09 src.attribute_error.AClass().attribute_09 src └── attribute_error.py └── class AClass(object): └── attribute_09 = attribute_08I 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 where the tests are running
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.py25class AClass(object): 26 27 attribute_00 = None 28 attribute_01 = None 29 attribute_02 = None 30 attribute_03 = None 31 attribute_04 = None 32 attribute_05 = None 33 attribute_06 = None 34 attribute_07 = None 35 attribute_08 = None 36 attribute_09 = None 37 38 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
25class AClass(object): 26 27 attribute_00 = None 28 attribute_01 = None 29 attribute_02 = None 30 attribute_03 = None 31 attribute_04 = None 32 attribute_05 = None 33 attribute_06 = None 34 attribute_07 = None 35 attribute_08 = None 36 attribute_09 = None 37 38 # method_00 = None 39 def method_00(): 40 return None
REFACTOR: make it better
I remove the commented line
25class AClass(object): 26 27 attribute_00 = None 28 attribute_01 = None 29 attribute_02 = None 30 attribute_03 = None 31 attribute_04 = None 32 attribute_05 = None 33 attribute_06 = None 34 attribute_07 = None 35 attribute_08 = None 36 attribute_09 = None 37 38 def method_00(): 39 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.py25class AClass(object): 26 27 attribute_00 = None 28 attribute_01 = None 29 attribute_02 = None 30 attribute_03 = None 31 attribute_04 = None 32 attribute_05 = None 33 attribute_06 = None 34 attribute_07 = None 35 attribute_08 = None 36 attribute_09 = None 37 38 def method_00(): 39 return None 40 41 def method_01(): 42 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.py25class AClass(object): 26 27 attribute_00 = None 28 attribute_01 = None 29 attribute_02 = None 30 attribute_03 = None 31 attribute_04 = None 32 attribute_05 = None 33 attribute_06 = None 34 attribute_07 = None 35 attribute_08 = None 36 attribute_09 = None 37 38 def method_00(): 39 return None 40 41 def method_01(): 42 return None 43 44 @staticmethod 45 def method_02(): 46 return None 47 48 def method_03(): 49 return None 50 51 @staticmethod 52 def method_04(): 53 return None 54 55 def method_05(): 56 return None 57 58 def method_06(): 59 return None 60 61 def method_07(): 62 return None 63 64 @staticmethod 65 def method_08(): 66 return None 67 68 def method_09(): 69 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
All the tests I have run for AttributeError show that because in Python everything is an object
I still have the problem that the tests all show the correct way to use attributes I made in attribute_error.py. If someone reads the file or runs it, there is no way for them to know how the code relates to AttributeError unless they go through the process with me, there has to be a better way.
code from the chapter
what is next?
You know
Would you like to know where the extra attributes and methods of the Person class came from?
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.