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 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_pythonfolderI open
test_attribute_error.pyI use pytest-watcher to run the tests automatically
the terminal is my friend, and shows
tests/test_attribute_error.py .. [100%] ================== 7 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
test_attribute_error.pythe terminal is my friend, and shows AttributeError
GREEN: make it pass
I open
attribute_error.pyI add a function to
attribute_error.pythe terminal is my friend, and shows AttributeError
I add a variable for
attribute_00to the functionthe 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 objectthe 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.pyI comment out the return statement from
AnObjectinattribute_error.pythe test passes because
attribute_00is now an attribute/property of theAnObjectobjectAnObjectis an attribute of theattribute_error.pymodule 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.py └── class AnObject(object): └── attribute_00 = function_09()
REFACTOR: make it better
I remove the commented lines
I add a line for
src.attribute_error.AnObject().attribute_01totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_01’. Did you mean: ‘attribute_00’?
I add
attribute_01to the object definition inattribute_error.pythe test passes because in this case it does not matter if I use the object (
AnObject) or an instance of the object (AnObject()).attribute_01is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_01 = attribute_00
I add a line for
src.attribute_error.AnObject.attribute_02totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_02’. Did you mean: ‘attribute_00’?
I add
attribute_02to the object definition inattribute_error.pythe test passes because
attribute_02is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_02 = attribute_01I add a line for
src.attribute_error.AnObject().attribute_03totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_03’. Did you mean: ‘attribute_00’?
I add
attribute_03to the object definition inattribute_error.pythe test passes because
attribute_03is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_03 = attribute_02I add a line for
src.attribute_error.AnObject.attribute_04totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_04’. Did you mean: ‘attribute_00’?
I add
attribute_04to the object definition inattribute_error.pythe test passes because
attribute_04is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_04 = attribute_03I add a line for
src.attribute_error.AnObject().attribute_05totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_05’. Did you mean: ‘attribute_00’?
I add
attribute_05to the object definition inattribute_error.pythe test passes because
attribute_05is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_05 = attribute_04I add a line for
src.attribute_error.AnObject().attribute_06totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_06’. Did you mean: ‘attribute_00’?
I add
attribute_06to the object definition inattribute_error.pythe test passes because
attribute_06is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_06 = attribute_05I add a line for
src.attribute_error.AnObject.attribute_07totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_07’. Did you mean: ‘attribute_00’?
I add
attribute_07to the object definition inattribute_error.pythe test passes because
attribute_07is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_07 = attribute_06I add a line for
src.attribute_error.AnObject.attribute_08totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_08’. Did you mean: ‘attribute_00’?
I add
attribute_08to the object definition inattribute_error.pythe test passes because
attribute_08is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_08 = attribute_07I add a line for
src.attribute_error.AnObject().attribute_09totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘attribute_09’. Did you mean: ‘attribute_00’?
I add
attribute_09to the object definition inattribute_error.pythe test passes because
attribute_09is now an attribute ofAnObjectinattribute_error.pyin thesrcfolder, 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.py └── class AnObject(object): └── attribute_09 = attribute_08
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
test_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_00’
GREEN: make it pass
I add the name to
AnObjectand point it to None, inattribute_error.pythe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘NoneType’ object is not callable
because
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
REFACTOR: make it better
I remove the commented line
You know the “drill”, I add a line for
src.attribute_error.AnObject.method_01totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_01’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe test passes because in this case it does not matter if I reference the method (
AnObject.method_01) or call it (AnObject.method_01()).I add a line for
src.attribute_error.AnObject.method_02totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_02’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe terminal is my friend, and shows TypeError
because this happens when
AnObject().method_02()is calledAnObject().method_02() AnObject.method_02(AnObject)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_02the test passes because this happens when
AnObject().method_02()is calledAnObject().method_02() AnObject.method_02(self)where
selfisAnObject.I add the staticmethod decorator method definition instead of
selfsinceit does not use anything that belongs to the object. That way I do not send more information than what the method needs.the test is still green because this now happens when
AnObject().method_02()is calledAnObject().method_02() AnObject.method_02()with the staticmethod decorator it does not matter if I call the method from an instance (
AnObject()) or from the object (AnObject).I remove the commented line
I add a line for
src.attribute_error.AnObject.method_03totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_03’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe test passes because in this case I reference the method (
AnObject().method_03), I do not call it (AnObject().method_03()).I add a line for
src.attribute_error.AnObject.method_04totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_04’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe terminal is my friend, and shows TypeError
because this happens when
AnObject().method_04()is calledAnObject().method_04() AnObject.method_04(AnObject)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_04the test passes because this now happens when
AnObject().method_04()is calledAnObject().method_04() AnObject.method_04()with the staticmethod decorator it does not matter if I call the method from an instance (
AnObject()) or from the object (AnObject).I add a line for
src.attribute_error.AnObject.method_05totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_05’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe test passes.
I add a line for
src.attribute_error.AnObject.method_06totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_06’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe test passes because this happens when
AnObject.method_06()is calledAnObject.method_06()I called the method with the object (
AnObject.method_06()) not an instance of the object (AnObject().method_06()).I add a line for
src.attribute_error.AnObject.method_07totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_07’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe test passes.
I add a line for
src.attribute_error.AnObject.method_08totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_08’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe terminal is my friend, and shows TypeError
because this happens when
AnObject().method_08()is calledAnObject().method_08() AnObject.method_08(AnObject)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 sinceit does not use anything that belongs to the object
the test passes because this now happens when
AnObject().method_08()is calledAnObject().method_08() AnObject.method_08()with the staticmethod decorator it does not matter if I call the method from an instance (
AnObject()) or from the object (AnObject).I add a line for
src.attribute_error.AnObject.method_09totest_attribute_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_09’. Did you mean: ‘method_00’?
I add the method to the definition of
AnObjectinattribute_error.pythe test passes because in this case I reference the method (
AnObject().method_09), I do not call it (AnObject().method_09()).A function in an object 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_errorthe 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?
Would you like to know where the extra attributes and methods of the Person object 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.