everything is an object
The object class is the mother of all things in Python.
what is a class?
I think of classes as attributes (variables) and methods (functions) that belong together (a classification).
what is a class attribute?
A class attribute is a variable that belongs to a class.
what is a method?
how to make a class
classes are made with
the class keyword
a name in CapWords format that tells what the group of attributes and methods does - naming things is its own challenge
class NameOfClass:
attribute = SOMETHING
def method():
the body of the method
return output
questions about classes
Questions to think about as I go through the chapter
preview
I have these tests by the end of the chapter
1class WPass: pass
2
3
4class WParentheses(): pass
5
6
7class WObject(object): pass
8
9
10def test_making_a_class_w_pass():
11 assert isinstance(WPass(), object)
12 assert issubclass(WPass, object)
13
14
15def test_making_a_class_w_parentheses():
16 assert isinstance(WParentheses(), object)
17 assert issubclass(WParentheses, object)
18
19
20def test_making_a_class_w_object():
21 assert isinstance(WObject(), object)
22 assert issubclass(WObject, object)
23
24
25def test_is_none_a_object():
26 assert isinstance(None, object)
27 # fails because None is not a class
28 # assert issubclass(None, object)
29
30
31def test_is_a_boolean_an_object():
32 assert isinstance(bool, object)
33 assert issubclass(bool, object)
34
35
36def test_is_an_integer_an_object():
37 assert isinstance(int, object)
38 assert issubclass(int, object)
39
40
41def test_is_a_float_an_object():
42 assert isinstance(float, object)
43 assert issubclass(float, object)
44
45
46def test_is_a_string_an_object():
47 assert isinstance(str, object)
48 assert issubclass(str, object)
49
50
51def test_is_a_tuple_an_object():
52 assert isinstance(tuple, object)
53 assert issubclass(tuple, object)
54
55
56def test_is_a_list_an_object():
57 assert isinstance(list, object)
58 assert issubclass(list, object)
59
60
61def test_is_a_set_an_object():
62 assert isinstance(set, object)
63 assert issubclass(set, object)
64
65
66def test_is_a_dictionary_an_object():
67 assert isinstance(dict, object)
68 assert issubclass(dict, object)
69
70
71def test_dir_object():
72 reality = dir(object)
73 my_expectation = [
74 '__class__', '__delattr__', '__dir__',
75 '__doc__', '__eq__', '__format__', '__ge__',
76 '__getattribute__', '__getstate__', '__gt__',
77 '__hash__', '__init__', '__init_subclass__',
78 '__le__', '__lt__', '__ne__', '__new__',
79 '__reduce__', '__reduce_ex__', '__repr__',
80 '__setattr__', '__sizeof__', '__str__',
81 '__subclasshook__'
82 ]
83 assert reality == my_expectation
84
85
86# Exceptions seen
87# AssertionError
88# NameError
89# TypeError
start the project
I name this project
classesI open a terminal
I change directory to the
classesfolder in thepumping_pythonfoldercd classesthe terminal shows
cd: no such file or directory: classesI use uv to make a directory for the project and initialize it
uv init classesthe terminal shows
Initialized project `classes` at `.../pumping_python/classes`I change directory to
classescd classesthe terminal shows I am in the
classesfolder.../pumping_python/classesI make a directory for the tests
mkdir testsI make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pyI use the mv program to change the name of
main.pytotest_classes.pyand move it to thetestsfoldermv main.py tests/test_classes.pyMove-Item main.py tests/test_classes.pyI open
test_classes.pyI delete the text in the file then add the first failing test to
test_classes.py1def test_failure(): 2 assert False is TrueI go back to the terminal to make a requirements file for the Python packages I need
echo "pytest" > requirements.txtI add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtI use uv to install pytest-watcher with the requirements file
uv add --requirement requirements.txtI add the new files and folder to git for tracking
git add .I add a git commit message
git commit -am 'setup project'I use
pytest-watcherto run the testsuv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
======================== FAILURES ======================== ______________________ test_failure ______________________ def test_failure(): > assert False is True E assert False is True test_classes.py:2: AssertionError ================ short test summary info ================= FAILED test_classes.py::test_failure - assert False is True =================== 1 failed in X.YZs ====================if the terminal does not show the same error, then check if
your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyyou ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
and try
uv run pytest-watcher . --nowagainI add AssertionError to the list of Exceptions seen
1def test_failure(): 2 assert False is True 3 4 5# Exceptions seen 6# AssertionErrorI change False to True in the assertion
1def test_failure(): 2 # assert False is True 3 assert True is True 4 5 6# Exceptions seen 7# AssertionErrorthe test passes.
how to test if something is NOT an instance
I can make a class with the class keyword, use CapWords format for the name and use a name that tells what the group of attributes and methods do.
class NameOfClass(ParentClass):
attribute = SOMETHING
def method():
the body of the method
...
I can test if an object is NOT an instance (a copy) of another object with the isinstance built-in function from The Python Standard Library.
isinstance checks if the thing in the parentheses on the left is an instance (a copy) of the class on the right in the parentheses.
test_making_a_class_w_pass
RED: make it fail
I change test_failure to test_making_a_class_w_pass then add an assertion with isinstance
1def test_making_a_class_w_pass(): 2 assert not isinstance(WPass(), object) 3 4 5# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'WPass' is not definedbecause
WPassis not defined in this file.I add NameError to the list of Exceptions seen
5# Exceptions seen 6# AssertionError 7# NameError
GREEN: make it pass
I add a class definition for WPass
1class WPass: pass
2
3
4def test_making_a_class_w_pass():
5 assert not isinstance(WPass(), object)
the terminal is my friend, and shows AssertionError
E assert not True
because the statement not isinstance(WPass(), object) is not True.
how to test if something is an instance
I can test if an object is an instance (a copy) of another object with the isinstance built-in function from The Python Standard Library.
isinstance checks if the thing in the parentheses on the left is an instance (a copy) of the class on the right in the parentheses.
I change the assertion to make the statement True
4def test_making_a_class_w_pass(): 5 # assert not isinstance(WPass(), object) 6 assert isinstance(WPass(), object) 7 8 9# Exceptions seenThe test passes because all classes inherit from ‘object’.
The assertion -
assert isinstance(WPass(), object)checks if the result of a call toWPassis an instance of the object class (the mother of all classes).The class definition simply says pass and the test passes.
pass is a special keyword that allows the class definition to follow Python language rules (the class must have a body).
REFACTOR: make it better
test_making_a_class_w_parentheses
I can also make a class with parentheses/brackets ( ).
RED: make it red
I go back to the terminal where the tests are running
I add another test to
test_classes.py4def test_making_a_class_w_pass(): 5 assert isinstance(WPass(), object) 6 7 8def test_making_a_class_w_parentheses(): 9 assert not isinstance(WParentheses(), object) 10 11 12# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'WParentheses' is not defined
GREEN: make it pass
I add a class definition for
WParentheseslike I did forWPass1class WPass: pass 2 3 4class WParentheses: pass 5 6 7def test_making_a_class_w_pass():the terminal is my friend, and shows AssertionError
AssertionError: assert not Truebecause the statement
not isinstance(WParentheses(), object)is not True.I change the assertion in test_making_a_class_w_parentheses to make the statement True
12def test_making_a_class_w_parentheses(): 13 # assert not isinstance(WParentheses(), object) 14 assert isinstance(WParentheses(), object) 15 16 17# Exceptions seenthe test passes.
REFACTOR: make it better
I add parentheses to the definition of
WParentheses4# class WParentheses: pass 5class WParentheses(): pass 6 7 8def test_making_a_class_w_pass():The test is still green because all classes inherit from ‘object’.
The assertion -
assert isinstance(WParentheses(), object)checks if the result of a call toWParenthesesis an instance of the object class (the mother of all classes).This class definition has parentheses after the name.
The class definition simply says pass and the test passes.
pass is a special keyword that allows the class definition to follow Python language rules (the class must have a body).
I remove the commented lines
1class WPass: pass 2 3 4class WParentheses(): pass 5 6 7def test_making_a_class_w_pass(): 8 assert isinstance(WPass(), object) 9 10 11def test_making_a_class_w_parentheses(): 12 assert isinstance(WParentheses(), object) 13 14 15# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_making_a_class_w_parentheses'
I can make a class with parentheses.
I have two classes with different statements, and the tests show that they are both instances of the object class
class WPass: pass
class WParentheses(): pass
because all classes inherit from ‘object’, which leads me to the next test.
test_making_a_class_w_object
I can make a class with object (the mother of all classes).
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for a new class in
test_classes.py11def test_making_a_class_w_parentheses(): 12 assert isinstance(WParentheses(), object) 13 14 15def test_making_a_class_w_object(): 16 assert not isinstance(WObject(), object) 17 18 19# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'WObject' is not defined
GREEN: make it pass
I add a class definition for
WObject4class WParentheses(): pass 5 6 7class WObject(): pass 8 9 10def test_making_a_class_w_pass():the terminal is my friend, and shows AssertionError
AssertionError: assert not Truebecause
not isinstance(WParentheses(), object)is not True.I change the assertion in test_making_a_class_w_object to make it True
18def test_making_a_class_w_object(): 19 # assert not isinstance(WObject(), object) 20 assert isinstance(WObject(), object) 21 22 23# Exceptions seenthe test passes.
REFACTOR: make it better
I add object to the parentheses of the class definition for
WObject7# class WObject(): pass 8class WObject(object): pass 9 10 11def test_making_a_class_w_pass():the test is still green.
I remove the commented lines
1class WPass: pass 2 3 4class WParentheses(): pass 5 6 7class WObject(object): pass 8 9 10def test_making_a_class_w_pass(): 11 assert isinstance(WPass(), object) 12 13 14def test_making_a_class_w_parentheses(): 15 assert isinstance(WParentheses(), object) 16 17 18def test_making_a_class_w_object(): 19 assert isinstance(WObject(), object) 20 21 22# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_making_a_class_w_object'
I have three different classes, and the tests show that they are all instances of the object class
class WPass: pass
class WParentheses(): pass
class WObject(object): pass
their results are the same because all classes inherit from ‘object’.
I like to write my classes with (object), so that anyone can see what the parent class is without thinking about it.
test_is_none_a_object
I want to test if None is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion
18def test_making_a_class_w_object(): 19 assert isinstance(WObject(), object) 20 21 22def test_is_none_a_object(): 23 assert not isinstance(None, object) 24 25 26# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not True
GREEN: make it pass
I change the statement to make it True
22 def test_is_none_a_object():
23 # assert not isinstance(None, object)
24 assert isinstance(None, object)
25
26
27 # Exceptions seen
the test passes.
REFACTOR: make it better
how to test if something is NOT a subclass
I can test if an object is NOT a subclass (child) of another object with the issubclass built-in function from The Python Standard Library.
issubclass checks if the thing in the parentheses on the left is a subclass of the class on the right in the parentheses.
test_is_a_boolean_an_object
I want to test if a boolean is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for bool (the class for booleans) to show that in Python everything is an object
22def test_is_none_a_object(): 23 assert isinstance(None, object) 24 25 26def test_is_a_boolean_an_object(): 27 assert not issubclass(bool, object) 28 29 30# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause bool is a child of object.
how to test if something is a subclass
I can test if an object is a subclass (child) of another object with the issubclass built-in function from The Python Standard Library.
issubclass checks if the thing in the parentheses on the left is a subclass of the class on the right in the parentheses.
GREEN: make it pass
I change the assertion to make it True
26 def test_is_a_boolean_an_object():
27 # assert not issubclass(bool, object)
28 assert issubclass(bool, object)
29
30
31 # Exceptions seen
the test passes.
REFACTOR: make it better
test_is_an_integer_an_object
I want to test if an integer (a whole number without decimals) is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for int (the class for whole numbers without decimals), to show that everything in Python is a child of object.
26def test_is_a_boolean_an_object(): 27 assert issubclass(bool, object) 28 29 30def test_is_an_integer_an_object(): 31 assert not issubclass(int, object) 32 33 34# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause int is a child of object.
GREEN: make it pass
I change the statement to make it True
30def test_is_an_integer_an_object():
31 # assert not issubclass(int, object)
32 assert issubclass(int, object)
33
34
35# Exceptions seen
the test passes.
REFACTOR: make it better
test_is_a_float_an_object
I want to test if a float (a binary floating point decimal number) is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for float (the class for binary floating point decimal numbers), to show that everything in Python is a child of object.
30def test_is_an_integer_an_object(): 31 assert issubclass(int, object) 32 33 34def test_is_a_float_an_object(): 35 assert not issubclass(float, object) 36 37 38# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause float is a child of object.
GREEN: make it pass
I change the statement to make it True
34 def test_is_a_float_an_object():
35 # assert not issubclass(float, object)
36 assert issubclass(float, object)
37
38
39 # Exceptions seen
the test passes.
REFACTOR: make it better
test_is_a_string_an_object
I want to test if a string (anything in quotes) is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for str (the class for anything in quotes), to show that in Python everything is an object
34def test_is_a_float_an_object(): 35 assert issubclass(float, object) 36 37 38def test_is_a_string_an_object(): 39 assert not issubclass(str, object) 40 41 42# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause str is a child of object.
GREEN: make it pass
I change the statement to make it True
38 def test_is_a_string_an_object():
39 # assert not issubclass(str, object)
40 assert issubclass(str, object)
41
42
43 # Exceptions seen
the test passes.
REFACTOR: make it better
test_is_a_tuple_an_object
I want to test if a tuple (anything in parentheses ( ) separated by a comma) is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for tuple (the class for anything in parentheses
( )separated by a comma), to show that in Python everything is an object38def test_is_a_string_an_object(): 39 assert issubclass(str, object) 40 41 42def test_is_a_tuple_an_object(): 43 assert not issubclass(tuple, object) 44 45 46# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause tuple is a child of object.
GREEN: make it pass
I change the statement to make it True
42def test_is_a_tuple_an_object():
43 # assert not issubclass(tuple, object)
44 assert issubclass(tuple, object)
45
46
47# Exceptions seen
the test passes.
REFACTOR: make it better
test_is_a_list_an_object
I want to test if a list (anything in square brackets [ ]) is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for list (the class for anything in square brackets ‘[ ]’), to show that in Python everything is an object
42def test_is_a_tuple_an_object(): 43 assert issubclass(tuple, object) 44 45 46def test_is_a_list_an_object(): 47 assert not issubclass(list, object) 48 49 50# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause list is a child of object.
GREEN: make it pass
I change the statement to make it True
46def test_is_a_list_an_object():
47 # assert not issubclass(list, object)
48 assert issubclass(list, object)
49
50
51# Exceptions seen
the test passes.
REFACTOR: make it better
test_is_a_set_an_object
I want to test if a set (anything in curly braces { }, not key-value pairs) is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for set (the class for anything in curly braces
{ }, not key-value pairs), to show that in Python everything is an object46def test_is_a_list_an_object(): 47 assert issubclass(list, object) 48 49 50def test_is_a_set_an_object(): 51 assert not issubclass(set, object) 52 53 54# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause set is a child of object.
GREEN: make it pass
I change the statement to make it True
50def test_is_a_set_an_object():
51 # assert not issubclass(set, object)
52 assert issubclass(set, object)
53
54
55# Exceptions seen
the test passes.
REFACTOR: make it better
test_is_a_dictionary_an_object
I want to test if a dictionary (any key-value pairs in curly braces { } separated by commas) is an object.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for dict (the class for key-value pairs in curly braces ‘{ }’ separated by commas), to show that in Python everything is an object
50def test_is_a_set_an_object(): 51 assert issubclass(set, object) 52 53 54def test_is_a_dictionary_an_object(): 55 assert not issubclass(dict, object) 56 57 58# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause dict is a child of object.
GREEN: make it pass
I change the statement to make it True
54def test_is_a_dictionary_an_object():
55 # assert not issubclass(dict, object)
56 assert issubclass(dict, object)
57
58
59# Exceptions seen
the test passes.
REFACTOR: make it better
instance vs subclass
An instance is a copy of an object and a subclass is a child of an object. They are different.
RED: make it fail
I add an assertion to test_making_a_class_w_pass to show that an instance (a copy) is different from a subclass (child)
10def test_making_a_class_w_pass(): 11 assert isinstance(WPass(), object) 12 assert issubclass(WPass(), object) 13 14 15def test_making_a_class_w_parentheses():the terminal is my friend, and shows TypeError
TypeError: issubclass() arg 1 must be a classbecause the first argument given in this call to the issubclass function is an instance not a class.
I add TypeError to the list of Exceptions seen
59# Exceptions seen 60# AssertionError 61# NameError 62# TypeError
GREEN: make it pass
I change the assertion to make the statement True
10def test_making_a_class_w_pass():
11 assert isinstance(WPass(), object)
12 # assert issubclass(WPass(), object)
13 assert issubclass(WPass, object)
14
15
16def test_making_a_class_w_parentheses():
the test passes.
REFACTOR: make it better
I remove the commented line from test_making_a_class_w_pass
10def test_making_a_class_w_pass(): 11 assert isinstance(WPass(), object) 12 assert issubclass(WPass, object) 13 14 15def test_making_a_class_w_parentheses():I add an assertion to test_making_a_class_w_parentheses to show that an instance (a copy) is different from a subclass (child)
15def test_making_a_class_w_parentheses(): 16 assert isinstance(WParentheses(), object) 17 assert issubclass(WParentheses(), object) 18 19 20def test_making_a_class_w_object():the terminal is my friend, and shows TypeError
TypeError: issubclass() arg 1 must be a classbecause
WParentheses()is an instance and the argument I put in the parentheses on the left should be a class.I change the assertion to make the statement True
15def test_making_a_class_w_parentheses(): 16 assert isinstance(WParentheses(), object) 17 # assert issubclass(WParentheses(), object) 18 assert issubclass(WParentheses, object) 19 20 21def test_making_a_class_w_object():the test passes.
I remove the commented line from test_making_a_class_w_parentheses
15def test_making_a_class_w_parentheses(): 16 assert isinstance(WParentheses(), object) 17 assert issubclass(WParentheses, object) 18 19 20def test_making_a_class_w_object():I add an assertion to test_making_a_class_w_object
20def test_making_a_class_w_object(): 21 assert isinstance(WObject(), object) 22 assert issubclass(WObject(), object) 23 24 25def test_is_none_a_object():the terminal is my friend, and shows TypeError
TypeError: issubclass() arg 1 must be a classI change the assertion to make the statement True
20def test_making_a_class_w_object(): 21 assert isinstance(WObject(), object) 22 # assert issubclass(WObject(), object) 23 assert issubclass(WObject, object) 24 25 26def test_is_none_a_object():the test passes.
I remove the commented line from test_making_a_class_w_object
25def test_making_a_class_w_object(): 26 assert isinstance(WObject(), object) 27 assert issubclass(WObject, object) 28 29 30def test_is_none_a_object():I add an assertion to test_is_none_a_object
25def test_is_none_a_object(): 26 assert isinstance(None, object) 27 assert issubclass(None, object) 28 29 30def test_is_a_boolean_an_object():the terminal is my friend, and shows TypeError
TypeError: issubclass() arg 1 must be a classI add a note and comment the line out
25def test_is_none_a_object(): 26 assert isinstance(None, object) 27 # fails because None is not a class 28 # assert issubclass(None, object) 29 30 31def test_is_a_boolean_an_object():I add an assertion to test_is_a_boolean_an_object
31def test_is_a_boolean_an_object(): 32 assert not isinstance(bool, object) 33 assert issubclass(bool, object) 34 35 36def test_is_an_integer_an_object():the terminal is my friend, and shows AssertionError
E assert not Truebecause
assert not isinstance(bool, object)is not True.I change the assertion to make it True
31def test_is_a_boolean_an_object(): 32 # assert not isinstance(bool, object) 33 assert isinstance(bool, object) 34 assert issubclass(bool, object) 35 36 37def test_is_an_integer_an_object():the test passes because bool is a
I remove the commented line from test_is_a_boolean_an_object
31def test_is_a_boolean_an_object(): 32 assert isinstance(bool, object) 33 assert issubclass(bool, object) 34 35 36def test_is_an_integer_an_object():I add an assertion to test_is_an_integer_an_object
36def test_is_an_integer_an_object(): 37 assert not isinstance(int, object) 38 assert issubclass(int, object) 39 40 41def test_is_a_float_an_object():the terminal is my friend, and shows AssertionError
E assert not Truebecause
assert not isinstance(int, object)is not True.I change the assertion
36def test_is_an_integer_an_object(): 37 # assert not isinstance(int, object) 38 assert isinstance(int, object) 39 assert issubclass(int, object) 40 41 42def test_is_a_float_an_object():the test passes because int is a
I remove the commented line from test_is_an_integer_an_object
36def test_is_an_integer_an_object(): 37 assert isinstance(int, object) 38 assert issubclass(int, object) 39 40 41def test_is_a_float_an_object():I add an assertion to test_is_a_float_an_object
41def test_is_a_float_an_object(): 42 assert not isinstance(float, object) 43 assert issubclass(float, object) 44 45 46def test_is_a_string_an_object():the terminal is my friend, and shows AssertionError
E assert not Truebecause
assert not isinstance(float, object)is not True.I change the assertion
41def test_is_a_float_an_object(): 42 # assert not isinstance(float, object) 43 assert isinstance(float, object) 44 assert issubclass(float, object) 45 46 47def test_is_a_string_an_object():the test passes because float is a
I remove the commented line from test_is_a_float_an_object
41def test_is_a_float_an_object(): 42 assert isinstance(float, object) 43 assert issubclass(float, object) 44 45 46def test_is_a_string_an_object():I add an assertion to test_is_a_string_an_object
46def test_is_a_string_an_object(): 47 assert not isinstance(str, object) 48 assert issubclass(str, object) 49 50 51def test_is_a_tuple_an_object():the terminal is my friend, and shows AssertionError
E assert not Truebecause
assert not isinstance(str, object)is not True.I change the assertion
46def test_is_a_string_an_object(): 47 # assert not isinstance(str, object) 48 assert isinstance(str, object) 49 assert issubclass(str, object) 50 51 52def test_is_a_tuple_an_object():the test passes because str is a
I remove the commented line from test_is_a_string_an_object
46def test_is_a_string_an_object(): 47 assert isinstance(str, object) 48 assert issubclass(str, object) 49 50 51def test_is_a_tuple_an_object():I add an assertion to test_is_a_tuple_an_object
51def test_is_a_tuple_an_object(): 52 assert not isinstance(tuple, object) 53 assert issubclass(tuple, object) 54 55 56def test_is_a_list_an_object():the terminal is my friend, and shows AssertionError
E assert not Truebecause
assert not isinstance(tuple, object)is not True.I change the assertion to make it True
51def test_is_a_tuple_an_object(): 52 # assert not isinstance(tuple, object) 53 assert isinstance(tuple, object) 54 assert issubclass(tuple, object) 55 56 57def test_is_a_list_an_object():the test passes because tuple is a
I remove the commented line from test_is_a_tuple_an_object
51def test_is_a_tuple_an_object(): 52 assert isinstance(tuple, object) 53 assert issubclass(tuple, object) 54 55 56def test_is_a_list_an_object():I add an assertion to test_is_a_list_an_object
56def test_is_a_list_an_object(): 57 assert not isinstance(list, object) 58 assert issubclass(list, object) 59 60 61def test_is_a_set_an_object():the terminal is my friend, and shows AssertionError
E assert not Truebecause
assert not isinstance(list, object)is not True.I change the assertion
56def test_is_a_list_an_object(): 57 # assert not isinstance(list, object) 58 assert isinstance(list, object) 59 assert issubclass(list, object) 60 61 62def test_is_a_set_an_object():the test passes because list is a
I remove the commented line from test_is_a_list_an_object
56def test_is_a_list_an_object(): 57 assert isinstance(list, object) 58 assert issubclass(list, object) 59 60 61def test_is_a_set_an_object():I add an assertion to test_is_a_set_an_object
61def test_is_a_set_an_object(): 62 assert not isinstance(set, object) 63 assert issubclass(set, object) 64 65 66def test_is_a_dictionary_an_object():the terminal is my friend, and shows AssertionError
E assert not Truebecause
assert not isinstance(set, object)is not True.I change the assertion
61def test_is_a_set_an_object(): 62 # assert not isinstance(set, object) 63 assert isinstance(set, object) 64 assert issubclass(set, object) 65 66 67def test_is_a_dictionary_an_object():the test passes because set is a
I remove the commented line from test_is_a_set_an_object
61def test_is_a_set_an_object(): 62 assert isinstance(set, object) 63 assert issubclass(set, object) 64 65 66def test_is_a_dictionary_an_object():I add an assertion to test_is_a_dictionary_an_object
66def test_is_a_dictionary_an_object(): 67 assert not isinstance(dict, object) 68 assert issubclass(dict, object) 69 70 71# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert not Truebecause
assert not isinstance(dict, object)is not True.I change the assertion to make it True
66def test_is_a_dictionary_an_object(): 67 # assert not isinstance(dict, object) 68 assert isinstance(dict, object) 69 assert issubclass(dict, object) 70 71 72# Exceptions seenthe test passes because dict is a
I remove the commented line from test_is_a_dictionary_an_object
66def test_is_a_dictionary_an_object(): 67 assert isinstance(dict, object) 68 assert issubclass(dict, object) 69 70 71# Exceptions seenI add a git commit message in the other terminal
git commit -am 'test instance vs subclass'
The difference between a subclass (child) I make, and an an instance (a copy) is the () after the name
a_name = ClassName
points the a_name variable to ClassName
a_name = ClassName()
points the a_name variable to the result of calling ClassName(). An an instance (a copy) is the result of calling the class.
test_dir_object
In test_dir_person_class I saw the methods I added to the Person class and also names that I did not add, which led to the question of where they came from.
I want to test the attributes and methods of the object class because it is the mother of all classes.
RED: make it fail
I go back to the terminal where the tests are running
I add a test to
test_classes.pywith a call to the dir built-in function to get the attributes and methods of object66def test_is_a_dictionary_an_object(): 67 assert isinstance(dict, object) 68 assert issubclass(dict, object) 69 70 71def test_dir_object(): 72 reality = dir(object) 73 my_expectation = [] 74 assert reality == my_expectation 75 76 77# Exceptions seenthe terminal is my friend, and shows AssertionError
E AssertionError: assert ['__class__',...ormat__', ...] == [] E E Left contains 24 more items, first extra item: '__class__' E Use -v to get more diff
GREEN: make it pass
I click in the terminal where the tests are running then press v on the keyboard for pytest-watcher to show me more of the difference between
realityandmy_expectationand it shows AssertionErrorE ...Full output truncated (24 lines hidden), use '-vv' to showI press w on the keyboard in the terminal where the tests are running, to show the menu for pytest-watcher and it shows
[pytest-watcher] Current runner args: [-v] Controls: > Enter : Invoke test runner > r : reset all runner args > c : change runner args > f : run only failed tests (--lf) > p : drop to pdb on fail (--pdb) > v : increase verbosity (-v) > e : Erase terminal screen > q : quit pytest-watcherI press c on the keyboard to change runner args, and the terminal shows
[pytest-watcher] Current runner args: [] Controls: > Enter : Invoke test runner > r : reset all runner args > c : change runner args > f : run only failed tests (--lf) > p : drop to pdb on fail (--pdb) > v : increase verbosity (-v) > e : Erase terminal screen > q : quit pytest-watcher Enter new runner args: -vvI press -+v+v on the keyboard then press enter to show the full difference, and the terminal shows AssertionError with the full list.
I copy (ctrl/command+c) the values from the terminal and paste (ctrl/command+v) them as
my_expectation71def test_dir_object(): 72 reality = dir(object) 73 my_expectation = [ 74 '__class__', '__delattr__', '__dir__', 75 '__doc__', '__eq__', '__format__', 76 '__ge__', '__getattribute__', 77 '__getstate__', '__gt__', '__hash__', 78 '__init__', '__init_subclass__', '__le__', 79 '__lt__', '__ne__', '__new__', '__reduce__', 80 '__reduce_ex__', '__repr__', '__setattr__', 81 '__sizeof__', '__str__', '__subclasshook__' 82 ] 83 assert reality == my_expectation 84 85 86# Exceptions seenThe test passes.
The __init__ method is in the list of attributes and methods
All classes automatically get these attributes, they inherit them because all classes inherit from ‘object’.
The __init__ method is also inherited which means when I defined it in test_classy_person_says_hello I overwrote the inherited one.
I add a git commit message in the other terminal
git commit -am \ 'add test_dir_object'
Everything in Python is an object because all classes inherit from ‘object’.
review
I can make a class with
Everything in Python is an object
close the project
I close
test_classes.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
classescd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
code from the chapter
what is next?
Would you like to test the other projects with classes?
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.