TypeError with objects
Since methods are functions in an object I can assume that they have the same behavior as what I tested in the functions project.
preview
I have these tests by the end of the chapter
questions about TypeError with objects
open the project
I open a terminal
I change directory to the type_error folder in the
pumping_pythonfolderI open
test_type_error.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
the terminal shows
test_type_error.py ... [100%] =================== 3 passed in G.HIs ====================
test_type_error_w_object_methods
RED: make it fail
I add a test with a call to AnObject.method from tests/test_type_error.py
83 src.type_error.function_08(
84 'positional',
85 argument='keyword',
86 )
87
88
89def test_type_error_w_object_methods():
90 src.type_error.AnObject.method_00()
91
92
93# Exceptions seen
the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error'
has no attribute 'AnObject'
because AnObject is not defined in src/type_error/__init__.py.
GREEN: make it pass
I open
__init__.pyfrom thetype_errorfolder in thesrcfolderI add an object definition for
AnObjecttosrc/type_error/__init__.pythe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'method_00'because there is nothing named
method_00inAnObject.I add the name to the object definition
the terminal is my friend, and shows NameError
NameError: name 'method_00' is not definedI define
method_00by pointing it to 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 change
method_00to a methodthe test passes.
REFACTOR: make it better
I remove the commented lines from
src/type_error/__init__.pyI add a call to
src.type_error.AnObject().method_01fromtests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_01'. Did you mean: 'method_00'?I add a method definition for
method_01toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_01() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the class (
self) it belongs to as the first argument.I add @staticmethod to
method_01class AnObject(object): def method_00(): return None @staticmethod def method_01(): return Nonethe test passes because I can use the staticmethod decorator if I do not want to add
selfto the method definition when it does not use anything that belongs to the object.Both methods look the same. The difference is in how I call them
AnObject.method_00()vsAnObject().method_01().I add a call to
src.type_error.AnObject().method_02fromtests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: 'AnObject' object has no attribute 'method_02'. Did you mean: 'method_00'?I add a definition for
method_02toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_02() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the class (
self) it belongs to as the first argument.I add @staticmethod to
method_02the terminal is my friend, and shows NameError
NameError: name 'self' is not definedI add
selfto the parentheses ofmethod_02the terminal is my friend, and shows TypeError
TypeError: AnObject.method_02() missing 1 required positional argument: 'self'I comment out the staticmethod decorator
the test passes because a method of an instance takes the instance of the class (
self) it belongs to as the first argument which allows it to use things that belong to the object.I add a call to
src.type_error.AnObject.method_03fromtests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'method_03'. Did you mean: 'method_00'?I add a definition for
method_03toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows NameError
NameError: name 'method_02' is not definedbecause there is nothing named
method_02insidemethod_03or at the module level ofsrc/type_error/__init__.py.I add
AnObject.beforemethod_02the terminal is my friend, and shows TypeError
TypeError: AnObject.method_02() missing 1 required positional argument: 'self'because
method_02takes an instance of the object it belongs to as input.I called the method with the object not an instance of the class.
I use an instance of the class to call the method
the test passes. This is a silly example because I used
AnObject()inside a method ofAnObject. I could just useself. I would only need this if I was calling a method of a different object.Here is another silly example. I add a call to
src.type_error.AnObject.method_04fromtests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'method_04'. Did you mean: 'method_00'?I add a method definition for
method_04toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_02() missing 1 required positional argument: 'self'because
method_02takes an instance of the object it belongs to as input.I called the method with the object not an instance of the class.
I pass
AnObjectas input in the call tomethod_02the test passes. I called a method of
AnObjectand passedAnObjectas input. I can useself.I add a call to
src.type_error.AnObject().method_05fromtests/test_type_error.pythe 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_05toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_05() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the class (
self) it belongs to as the first argument.I add
selfto the parentheses ofmethod_05the terminal is my friend, and shows NameError
NameError: name 'method_02' is not definedbecause there is no
method_02at the module level ofsrc/type_error/__init__.py. It is insideAnObjectinsrc/type_error/__init__.py, I have to be specific.I add
self.beforemethod_02the test passes.
I add a call to
src.type_error.AnObject().method_06fromtests/test_type_error.pythe 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_06toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_06() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the class (
self) it belongs to as the first argument.I add
selfto the parentheses ofmethod_06the test passes.
I add a call to
src.type_error.AnObject().method_07fromtests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'method_07'. Did you mean: 'method_00'?I add a method definition for
method_07toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_07() missing 1 required positional argument: 'self'because
method_07takes an instance of the object it belongs to as input.I called the method with the object not an instance of the class.
I use an instance of the class to call the method from
tests/test_type_error.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_00() takes 0 positional arguments but 1 was givenbecause
method_00takes no input (the parentheses are empty).I called it with an instance of the class (
AnObject()) which passes the instance as input.
I add the staticmethod decorator to
method_00ofAnObjectinsrc/type_error/__init__.pythe test passes. I can use the staticmethod decorator if I do not want to add
selfto the method definition when it does not use anything that belongs to the object.I add a call to
src.type_error.AnObject.method_08()fromtests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: type object 'AnObject' has no attribute 'method_08'. Did you mean: 'method_00'?I add a method definition for
method_08toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_08() missing 1 required positional argument: 'self'because I called
method_08like a staticmethod and it is defined as an instance method (It expectsselfas input).I use an instance of the class to call the method from
tests/test_type_error.pyTypeError: AnObject.method_04() takes 0 positional arguments but 1 was givenbecause
method_04takes no input (the parentheses are empty).I called it with an instance of the class (
AnObject()) which passes the instance as input.
I add the staticmethod decorator to
method_04ofAnObjectinsrc/type_error/__init__.pythe test passes.
I add a call to
src.type_error.AnObject().method_09()fromtests/test_type_error.pythe 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_09toAnObjectinsrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: AnObject.method_03() takes 0 positional arguments but 1 was givenbecause
method_03takes no input (the parentheses are empty).I called it with an instance of the class (
AnObject()) which passes the instance as input.
I add the @staticmethod to
method_03ofAnObjectinsrc/type_error/__init__.pythe test passes.
I remove the commented lines from
src/type_error/__init__.pyI remove the commented lines from
tests/test_type_error.pyI open a new terminal then make sure I am in the
type_errorfolder
test_type_error_w_the_uncallables
Is every object callable?
RED: make it fail
I go back to the terminal that is running the tests.
I add a test to
tests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'none'there is nothing named
noneinsrc/type_error/__init__.pyin thesrcfolder, yet.
GREEN: make it pass
I add
noneand point it to None1none = Nonethe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablethe
()to the right ofsrc.type_error.nonemakes it a callthe name
nonepoints to None which is NOT callable. Using substitutionnone = None # point the name to the object none() # call the name None() # substitute the value for the nameNone()raises TypeError because I cannot call None like a function.
I make
nonea function insrc/type_error/__init__.pyto make it callable1# none = None 2def none(): return None 3 4 5def function_00(the_input): 6 return Nonethe test passes.
I can call a function, I cannot call None.
REFACTOR: make it better
I add a call to
falseintests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'false'falseis not insrc/type_error/__init__.py.I add
falsetosrc/type_error/__init__.pyand point it to False1# none = None 2def none(): return None 3false = False 4 5 6def function_00(the_input): 7 return Nonethe terminal is my friend, and shows TypeError
TypeError: 'bool' object is not callablefalsepoints to False which is NOT callable. Using substitutionfalse = False false() False()False()raises TypeError because I cannot call a boolean like a function.I change
falsefrom a variable to a function to make it callable1# none = None 2def none(): return None 3# false = False 4def false(): return False 5 6 7def function_00(the_input): 8 return Nonethe test is green again.
I add a call to the other boolean in
tests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'true'there is nothing named
trueinsrc/type_error/__init__.py.I add
trueand point it to True insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: 'bool' object is not callableI change
truefrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a boolean or None.
I add a call to an integer, in
tests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'an_integer'I add
an_integerand point it to1234insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: 'int' object is not callablethe name
an_integerpoints to an integer (1234) which is NOT callable. Using substitutionan_integer = 1234 # point the name to the object an_integer() # call the name 1234() # substitute the value for the name1234()raises TypeError because I cannot call an integer like a function.I change
an_integerfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call an integer, a boolean or None.
I add a call to a float in
tests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'a_float'I add
a_floatand point it to5.678insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: 'float' object is not callablea_floatpoints to a float (5.678) which is NOT callable. Using substitutiona_float = 5.678 a_float() 5.678()5.678()raises TypeError because I cannot call a float like a function.I change
a_floatfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a float, integer, boolean or None.
I add a call to a string (anything in quotes) in
tests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'a_string'I add
a_stringand point it to'a string'insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: 'str' object is not callablethe name
a_stringpoints to a string ('a string') which is NOT callable. Using substitutiona_string = 'a string' # point the name to the object a_string() # call the name 'a string'() # substitute the value for the name'a string'()raises TypeError because I cannot call a string like a function.I change
a_stringfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a string, float, integer, boolean or None.
I add a call to a tuple (anything in parentheses
(), separated by a comma) intests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'a_tuple'I add
a_tupleand point it to(0, 1, 2, 'n')insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: 'tuple' object is not callablethe name
a_tuplepoints to a tuple ((0, 1, 2, 'n')) which is NOT callable. Using substitutiona_tuple = (0, 1, 2, 'n') a_tuple() (0, 1, 2, 'n')()(0, 1, 2, 'n')()raises TypeError because I cannot call a tuple like a function.I change
a_tuplefrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a tuple, string, float, integer, boolean or None.
I add a call to a list in
tests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'a_list'I add
a_listand point it to[0, 1, 2, 'n']insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: 'list' object is not callablea_listpoints to a list ([0, 1, 2, 'n']) which is NOT callable. Using substitutiona_list = [0, 1, 2, 'n'] a_list() [0, 1, 2, 'n']()[0, 1, 2, 'n']()raises TypeError because I cannot call a list like a function.I change
a_listfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a list, tuple, string, float, integer, boolean or None.
I add a call to a set in
tests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'a_set'I add
a_setand point it to{0, 1, 2, 'n'}insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: 'set' object is not callablea_setpoints to a set ({0, 1, 2, 'n'}) which is NOT callable. Using substitutiona_set = {0, 1, 2, 'n'} a_set() {0, 1, 2, 'n'}(){0, 1, 2, 'n'}()raises TypeError because I cannot call a set like a function.I change
a_setfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a set, list, tuple, string, float, integer, boolean or None.
I add a call to a dictionary in
tests/test_type_error.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'a_dictionary'I add
a_dictionaryand point it to{'key': 'value'}insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: 'dict' object is not callablea_dictionarypoints to a dictionary ({'key': 'value'}) which is NOT callable. Using substitutiona_dictionary = {'key': 'value'} a_dictionary() {'key': 'value'}(){'key': 'value'}()raises TypeError because I cannot call a dictionary like a function.I change
a_dictionaryfrom a variable to a function to make it callablethe test is green again. I can call a function, I cannot call a dictionary, set, list, tuple, string, float, integer, boolean or None.
I remove the commented lines from
src/type_error/__init__.py1def none(): return None 2def false(): return False 3def true(): return True 4def an_integer(): return 1234 5def a_float(): return 5.678 6def a_string(): return 'a string' 7def a_tuple(): return (0, 1, 2, 'n') 8def a_list(): return [0, 1, 2, 'n'] 9def a_set(): return {0, 1, 2, 'n'} 10def a_dictionary(): return {'key': 'value'} 11 12 13def function_00(the_input): 14 return None
close the project
I close
tests/test_type_error.pyandsrc/type_error/__init__.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
type_errorthe terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
The tests show that
If I call a method with an instance, it takes the instance as the first argument (
self).I can use the staticmethod decorator if the method does not use anything that belongs to the object it is part of.
All the tests so far show that I get TypeError when I call an object in a way that is different from its definition.
How many questions can you answer about TypeError with objects?
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.