everything is an object
The object class is the mother of all things in Python.
questions about classes
Questions to think about as I go through the chapter
preview
I have these tests by the end of the chapter
1import src.classes
2import unittest
3
4
5class TestClasses(unittest.TestCase):
6
7 def test_making_a_class_w_pass(self):
8 an_instance = src.classes.WPass()
9 assert isinstance(an_instance, object)
10 self.assertIsInstance(an_instance, object)
11
12 a_class = src.classes.WPass
13 assert issubclass(a_class, object)
14 self.assertIsSubclass(a_class, object)
15
16 def test_making_a_class_w_parentheses(self):
17 an_instance = src.classes.WParentheses()
18 assert isinstance(an_instance, object)
19 self.assertIsInstance(an_instance, object)
20
21 a_class = src.classes.WParentheses
22 assert issubclass(a_class, object)
23 self.assertIsSubclass(a_class, object)
24
25 def test_making_a_class_w_object(self):
26 an_instance = src.classes.WObject()
27 assert isinstance(an_instance, object)
28 self.assertIsInstance(an_instance, object)
29
30 a_class = src.classes.WObject
31 assert issubclass(a_class, object)
32 self.assertIsSubclass(a_class, object)
33
34 def test_is_none_an_object(self):
35 assert isinstance(None, object)
36 self.assertIsInstance(None, object)
37
38 def test_is_a_boolean_an_object(self):
39 assert issubclass(bool, object)
40 self.assertIsSubclass(bool, object)
41
42 def test_is_an_integer_an_object(self):
43 assert issubclass(int, object)
44 self.assertIsSubclass(int, object)
45
46 def test_is_a_float_an_object(self):
47 assert issubclass(float, object)
48 self.assertIsSubclass(float, object)
49
50 def test_is_a_string_an_object(self):
51 assert issubclass(str, object)
52 self.assertIsSubclass(str, object)
53
54 def test_is_a_tuple_an_object(self):
55 assert issubclass(tuple, object)
56 self.assertIsSubclass(tuple, object)
57
58 def test_is_a_list_an_object(self):
59 assert issubclass(list, object)
60 self.assertIsSubclass(list, object)
61
62 def test_is_a_set_an_object(self):
63 assert issubclass(set, object)
64 self.assertIsSubclass(set, object)
65
66 def test_is_a_dictionary_an_object(self):
67 assert issubclass(dict, object)
68 self.assertIsSubclass(dict, object)
69
70 def test_attributes_and_methods_of_objects(self):
71 reality = dir(object)
72 my_expectation = [
73 '__class__',
74 '__delattr__',
75 '__dir__',
76 '__doc__',
77 '__eq__',
78 '__format__',
79 '__ge__',
80 '__getattribute__',
81 '__getstate__',
82 '__gt__',
83 '__hash__',
84 '__init__',
85 '__init_subclass__',
86 '__le__',
87 '__lt__',
88 '__ne__',
89 '__new__',
90 '__reduce__',
91 '__reduce_ex__',
92 '__repr__',
93 '__setattr__',
94 '__sizeof__',
95 '__str__',
96 '__subclasshook__'
97 ]
98 self.assertEqual(reality, my_expectation)
99
100
101# Exceptions seen
102# AssertionError
103# NameError
104# AttributeError
105# TypeError
requirements
continue the project
I change directory to the
personfoldercd personthe terminal shows I am in the
personfolder.../pumping_python/personI make a new file in the
testsfolder namedtest_classes.pytouch tests/test_classes.pyI make a new file in the
srcfolder namedclasses.pytouch src/classes.pyI open
test_classes.pyI add the first failing test to
test_classes.py1import unittest 2 3 4class TestClasses(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True)I go back to the terminal to add the new files and folders to git for tracking
git add .the terminal goes back to the command line.
I use
pytest-watcherto run the testsuv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
============================ FAILURES ========================== ___________________ TestClasses.test_failure ___________________ self = <tests.test_classes.TestClasses testMethod=test_failure> def test_failure(self): > self.assertFalse(True) E AssertionError: True is not false tests/test_classes.py:7: AssertionError =================== short test summary info ==================== FAILED tests/test_classes.py::TestClasses::test_failure - AssertionError: True is not false ================= 1 failed, 6 passed in X.YZs ==================I add AssertionError to the list of Exceptions seen in
test_functions.py4class TestClasses(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True) 8 9 10# Exceptions seen 11# AssertionErrorthen I change True to False in the assertion
7 self.assertFalse(False)the test passes.
test_making_a_class_w_pass
To review, 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
...
RED: make it fail
how to test if something is NOT an instance of a class
I can test if an object is an instance (a copy) of another object or NOT 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 test_failure to test_making_a_class_w_pass then add an assertion with isinstance
4class TestClasses(unittest.TestCase): 5 6 def test_making_a_class_w_pass(self): 7 assert not isinstance( 8 src.classes.WPass(), object 9 ) 10 11 12# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'src' is not definedbecause
srcis not defined in this fileI add NameError to the list of Exceptions seen
12# Exceptions seen 13# AssertionError 14# NameError
GREEN: make it pass
I add an import statement for the
classesmodule1import src.classes 2import unittest 3 4 5class TestClasses(unittest.TestCase):import src.classesbrings in an object for theclasses.pymodule from thesrcfolder so I can use it intest_classes.pythe terminal is my friend, and shows AttributeError
AttributeError: module 'src.classes' has no attribute 'WPass'because there is no definition for
WPassinclasses.py
I add AttributeError to the list of Exceptions seen
13# Exceptions seen 14# AssertionError 15# NameError 16# AttributeErrorI open
classes.pyfrom thesrcfolderthen I add a class definition for
WPasstoclasses.py1class WPass: passthe terminal is my friend, and shows AssertionError
AssertionError: assert not Truebecause the statement
not isinstance(src.classes.WPass(), object)is False.
how to test if something is an instance of a class
I change the assertion to make the statement True, in
test_classes.py7 def test_making_a_class_w_pass(self): 8 # assert not isinstance( 9 assert isinstance( 10 src.classes.WPass(), object 11 ) 12 13 14# Exceptions seenthe assertion -
assert isinstance(src.classes.WPass(), object)checks if the result of a call toWPassinsrc.classes.pyin thesrcfolder also known assrc.classes.WPass(), is 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)
the test passes because all classes inherit from ‘object’
REFACTOR: make it better
The unittest.TestCase class has two methods I can also use to test if an object is an instance (a copy) of a class or NOT - assertIsInstance and assertNotIsInstance
another way to test if something is NOT an instance of a class
I add the assertNotIsInstance method to the test
7 def test_making_a_class_w_pass(self):
8 # assert not isinstance(
9 assert isinstance(
10 src.classes.WPass(), object
11 )
12 self.assertNotIsInstance(
13 src.classes.WPass(), object
14 )
15
16
17# Exceptions seen
the terminal is my friend, and shows AssertionError
AssertionError:
<src.classes.WPass() object at 0xffff8a7b6543>
is an instance of <class 'object'>
another way to test if something is an instance of a class
I change assertNotIsInstance to assertIsInstance
7 def test_making_a_class_w_pass(self): 8 # assert not isinstance( 9 assert isinstance( 10 src.classes.WPass(), object 11 ) 12 # self.assertNotIsInstance( 13 self.assertIsInstance( 14 src.classes.WPass(), object 15 ) 16 17 18# Exceptions seenthe test passes.
I add a variable
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 # assert not isinstance( 10 assert isinstance( 11 src.classes.WPass(), object 12 ) 13 # self.assertNotIsInstance( 14 self.assertIsInstance( 15 src.classes.WPass(), object 16 ) 17 18 19# Exceptions seenI use the variable to remove repetition of
src.classes.WPass()7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 # assert not isinstance( 12 # assert isinstance( 13 # src.classes.WPass(), object 14 # ) 15 # self.assertNotIsInstance( 16 # self.assertIsInstance( 17 # src.classes.WPass(), object 18 # ) 19 20 21# Exceptions seenthe test is still green.
I remove the commented lines
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 12 13# Exceptions seenI open a new terminal, then add a git commit message
git commit -am \ 'add test_making_a_class_w_pass'
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 that is running the tests
I add another test
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 12 def test_making_a_class_w_parentheses(self): 13 assert not isinstance( 14 src.classes.WParentheses(), object 15 ) 16 17 18# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.classes' has no attribute 'WParentheses'
GREEN: make it pass
I add a class definition like
WPasstoclasses.py1class WPass: pass 2 3 4class WParentheses: passthe terminal is my friend, and shows AssertionError
AssertionError: assert not Truebecause the statement
not isinstance(src.classes.WParentheses(), object)is False.I change the assertion in test_making_a_class_w_parentheses to make the statement True, in
test_classes.py12 def test_making_a_class_w_parentheses(self): 13 # assert not isinstance( 14 assert isinstance( 15 src.classes.WParentheses(), object 16 ) 17 18 19# Exceptions seenthe test passes.
REFACTOR: make it better
I add parentheses to the definition of
WParenthesesinclasses.py4# class WParentheses: pass 5class WParentheses(): passthe test is still green because all classes inherit from ‘object’
the assertion -
assert isinstance(src.classes.WParentheses(), object)checks if the result of a call toWParenthesesinsrc.classes.pyin thesrcfolder also known assrc.classes.WParentheses(), is 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 line
1class WPass: pass 2 3 4class WParentheses(): passI add the assertNotIsInstance method to test_making_a_class_w_parentheses in
test_classes.py12 def test_making_a_class_w_parentheses(self): 13 # assert not isinstance( 14 assert isinstance( 15 src.classes.WParentheses(), object 16 ) 17 self.assertNotIsInstance( 18 src.classes.WParentheses(), object 19 ) 20 21 22# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <src.classes.WParentheses() object at 0xffffab123456> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
12 def test_making_a_class_w_parentheses(self): 13 # assert not isinstance( 14 assert isinstance( 15 src.classes.WParentheses(), object 16 ) 17 # self.assertNotIsInstance( 18 self.assertIsInstance( 19 src.classes.WParentheses(), object 20 ) 21 22 23# Exceptions seenthe test passes.
I add a variable
12 def test_making_a_class_w_parentheses(self): 13 an_instance = src.classes.WParentheses() 14 # assert not isinstance( 15 assert isinstance( 16 src.classes.WParentheses(), object 17 ) 18 # self.assertNotIsInstance( 19 self.assertIsInstance( 20 src.classes.WParentheses(), object 21 ) 22 23 24# Exceptions seenI use the variable to remove repetition of
src.classes.WParentheses()12 def test_making_a_class_w_parentheses(self): 13 an_instance = src.classes.WParentheses() 14 assert isinstance(an_instance, object) 15 self.assertIsInstance(an_instance, object) 16 # assert not isinstance( 17 # assert isinstance( 18 # src.classes.WParentheses(), object 19 # ) 20 # self.assertNotIsInstance( 21 # self.assertIsInstance( 22 # src.classes.WParentheses(), object 23 # ) 24 25 26# Exceptions seenthe test is still green.
I remove the commented lines
12 def test_making_a_class_w_parentheses(self): 13 an_instance = src.classes.WParentheses() 14 assert isinstance(an_instance, object) 15 self.assertIsInstance(an_instance, object) 16 17 18# 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 that is running the tests
I add a test with an assertion for a new class
12 def test_making_a_class_w_parentheses(self): 13 an_instance = src.classes.WParentheses() 14 assert isinstance(an_instance, object) 15 self.assertIsInstance(an_instance, object) 16 17 def test_making_a_class_w_object(self): 18 assert not isinstance( 19 src.classes.WObject(), object 20 ) 21 22 23# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.classes' has no attribute 'WObject'
GREEN: make it pass
I add the class definition to
classes.py4class WParentheses(): pass 5 6 7class WObject(): passthe terminal is my friend, and shows AssertionError
AssertionError: assert not TrueI change the statement of the assertion in test_making_a_class_w_object to make it True
17 def test_making_a_class_w_object(self): 18 # assert not isinstance( 19 assert isinstance( 20 src.classes.WObject(), object 21 ) 22 23 24# Exceptions seenthe test passes.
REFACTOR: make it better
I add object to the parentheses of the class definition for
WObjectinclasses.py7# class WObject(): pass 8class WObject(object): passthe test is still green.
I remove the commented line
4class WParentheses(): pass 5 6 7class WObject(object): passI add an assertion with the assertNotIsInstance method to test_making_a_class_w_object in
test_classes.py17 def test_making_a_class_w_object(self): 18 # assert not isinstance( 19 assert isinstance( 20 src.classes.WObject(), object 21 ) 22 self.assertNotIsInstance( 23 src.classes.WObject(), object 24 ) 25 26 27# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <src.classes.WObject() object at 0xffffcd781234> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
17 def test_making_a_class_w_object(self): 18 # assert not isinstance( 19 assert isinstance( 20 src.classes.WObject(), object 21 ) 22 # self.assertNotIsInstance( 23 self.assertIsInstance( 24 src.classes.WObject(), object 25 ) 26 27 28# Exceptions seenthe test passes.
I add a variable to remove repetition of
src.classes.WObject()17 def test_making_a_class_w_object(self): 18 an_instance = src.classes.WObject() 19 # assert not isinstance( 20 assert isinstance( 21 src.classes.WObject(), object 22 ) 23 # self.assertNotIsInstance( 24 self.assertIsInstance( 25 src.classes.WObject(), object 26 ) 27 28 29# Exceptions seenI use the variable to remove repetition of
src.classes.WObject()17 def test_making_a_class_w_object(self): 18 an_instance = src.classes.WObject() 19 assert isinstance(an_instance, object) 20 self.assertIsInstance(an_instance, object) 21 # assert not isinstance( 22 # assert isinstance( 23 # src.classes.WObject(), object 24 # ) 25 # self.assertNotIsInstance( 26 # self.assertIsInstance( 27 # src.classes.WObject(), object 28 # ) 29 30 31# Exceptions seenthe test is still green.
I remove the commented lines
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 12 def test_making_a_class_w_parentheses(self): 13 an_instance = src.classes.WParentheses() 14 assert isinstance(an_instance, object) 15 self.assertIsInstance(an_instance, object) 16 17 def test_making_a_class_w_object(self): 18 an_instance = src.classes.WObject() 19 assert isinstance(an_instance, object) 20 self.assertIsInstance(an_instance, object) 21 22 23# 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 definitions are different, 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_an_object
I want to test if None is an object.
RED: make it fail
I go back to the terminal that is running the tests
I add a test with an assertion
17 def test_making_a_class_w_object(self): 18 an_instance = src.classes.WObject() 19 assert isinstance(an_instance, object) 20 self.assertIsInstance(an_instance, object) 21 22 def test_is_none_an_object(self): 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_an_object(self):
23 # assert not isinstance(None, object)
24 assert isinstance(None, object)
25
26
27# Exceptions seen
the test passes.
REFACTOR: make it better
I use assertNotIsInstance to show that None is a child of object
22 def test_is_none_an_object(self): 23 # assert not isinstance(None, object) 24 assert isinstance(None, object) 25 self.assertNotIsInstance(None, object) 26 27 28# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: None is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
22 def test_is_none_an_object(self): 23 # assert not isinstance(None, object) 24 assert isinstance(None, object) 25 # self.assertNotIsInstance(None, object) 26 self.assertIsInstance(None, object) 27 28 29# Exceptions seenthe test passes.
I remove the commented lines
22 def test_is_none_an_object(self): 23 assert isinstance(None, object) 24 self.assertIsInstance(None, object) 25 26 27# Exceptions seenI add a git commit message in the other terminal
git commit -am 'add test_is_none_an_object'
test_is_a_boolean_an_object
I want to test if a boolean is an object.
RED: make it fail
how to test if something is NOT a subclass of a class
I can test if an object is a subclass (child) of another object or NOT 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.
I go back to the terminal that is running the tests
I add a test with an assertion for bool to show that in Python everything is an object
22 def test_is_none_an_object(self): 23 assert isinstance(None, object) 24 self.assertIsInstance(None, object) 25 26 def test_is_a_boolean_an_object(self): 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.
GREEN: make it pass
how to test if something is a subclass of a class
I change the statement to make it True
26 def test_is_a_boolean_an_object(self):
27 # assert not issubclass(bool, object)
28 assert issubclass(bool, object)
29
30
31# Exceptions seen
the test passes.
REFACTOR: make it better
The unittest.TestCase class has 2 methods I can also use to test if an object is a subclass (child) of a class or NOT - assertIsSubclass and assertNotIsSubclass.
another way to test if something is NOT a subclass of a class
I use unittest.TestCase.assertNotIsSubclass to show that bool (the class for booleans) is a child of object
26 def test_is_a_boolean_an_object(self):
27 # assert not issubclass(bool, object)
28 assert issubclass(bool, object)
29 self.assertNotIsSubclass(bool, object)
30
31
32# Exceptions seen
the terminal is my friend, and shows AssertionError
AssertionError:
<class 'bool'> is a subclass of <class 'object'>
because bool is a child of object.
another way to test if something is a subclass of a class
I change assertNotIsSubclass to assertIsSubclass
26 def test_is_a_boolean_an_object(self): 27 # assert not issubclass(bool, object) 28 assert issubclass(bool, object) 29 # self.assertNotIsSubclass(bool, object) 30 self.assertIsSubclass(bool, object) 31 32 33# Exceptions seenthe test passes.
I remove the commented lines
26 def test_is_a_boolean_an_object(self): 27 assert issubclass(bool, object) 28 self.assertIsSubclass(bool, object) 29 30 31# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_is_a_boolean_an_object'
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 that is running the tests
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.
26 def test_is_a_boolean_an_object(self): 27 assert issubclass(bool, object) 28 self.assertIsSubclass(bool, object) 29 30 def test_is_an_integer_an_object(self): 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
30 def test_is_an_integer_an_object(self):
31 # assert not issubclass(int, object)
32 assert issubclass(int, object)
33
34
35# Exceptions seen
the test passes.
REFACTOR: make it better
I use assertNotIsSubclass to show that int is a child of object
30 def test_is_an_integer_an_object(self): 31 # assert not issubclass(int, object) 32 assert issubclass(int, object) 33 self.assertNotIsSubclass(int, object) 34 35 36# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <class 'int'> is a subclass of <class 'object'>because int is a child of object.
I change assertNotIsSubclass to assertIsSubclass
30 def test_is_an_integer_an_object(self): 31 # assert not issubclass(int, object) 32 assert issubclass(int, object) 33 # self.assertNotIsSubclass(int, object) 34 self.assertIsSubclass(int, object) 35 36 37# Exceptions seenthe test passes.
I remove the commented lines
30 def test_is_an_integer_an_object(self): 31 assert issubclass(int, object) 32 self.assertIsSubclass(int, object) 33 34 35# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_is_an_integer_an_object'
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 that is running the tests
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.
30 def test_is_an_integer_an_object(self): 31 assert issubclass(int, object) 32 self.assertIsSubclass(int, object) 33 34 def test_is_a_float_an_object(self): 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(self):
35 # assert not issubclass(float, object)
36 assert issubclass(float, object)
37
38
39# Exceptions seen
the test passes.
REFACTOR: make it better
I use assertNotIsSubclass to show that float is a child of object
34 def test_is_a_float_an_object(self): 35 # assert not issubclass(float, object) 36 assert issubclass(float, object) 37 self.assertNotIsSubclass(float, object) 38 39 40# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <class 'float'> is a subclass of <class 'object'>because float is a child of object.
I change assertNotIsSubclass to assertIsSubclass
34 def test_is_a_float_an_object(self): 35 # assert not issubclass(float, object) 36 assert issubclass(float, object) 37 # self.assertNotIsSubclass(float, object) 38 self.assertIsSubclass(float, object) 39 40 41# Exceptions seenthe test passes.
I remove the commented lines
34 def test_is_a_float_an_object(self): 35 assert issubclass(float, object) 36 self.assertIsSubclass(float, object) 37 38 39# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_is_a_float_an_object'
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 that is running the tests
I add a test with an assertion for str (the class for anything in quotes), to show that in Python everything is an object
34 def test_is_a_float_an_object(self): 35 assert issubclass(float, object) 36 self.assertIsSubclass(float, object) 37 38 def test_is_a_string_an_object(self): 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(self):
39 # assert not issubclass(str, object)
40 assert issubclass(str, object)
41
42
43# Exceptions seen
the test passes.
REFACTOR: make it better
I use assertNotIsSubclass to show that str is a child of object
38 def test_is_a_string_an_object(self): 39 # assert not issubclass(str, object) 40 assert issubclass(str, object) 41 self.assertNotIsSubclass(str, object) 42 43 44# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <class 'str'> is a subclass of <class 'object'>because str is a child of object.
I change assertNotIsSubclass to assertIsSubclass
38 def test_is_a_string_an_object(self): 39 # assert not issubclass(str, object) 40 assert issubclass(str, object) 41 # self.assertNotIsSubclass(str, object) 42 self.assertIsSubclass(str, object) 43 44 45# Exceptions seenthe test passes.
I remove the commented lines
38 def test_is_a_string_an_object(self): 39 assert issubclass(str, object) 40 self.assertIsSubclass(str, object) 41 42 43# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_is_a_string_an_object'
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 that is running the tests
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 object38 def test_is_a_string_an_object(self): 39 assert issubclass(str, object) 40 self.assertIsSubclass(str, object) 41 42 def test_is_a_tuple_an_object(self): 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
42 def test_is_a_tuple_an_object(self):
43 # assert not issubclass(tuple, object)
44 assert issubclass(tuple, object)
45
46
47# Exceptions seen
the test passes.
REFACTOR: make it better
I use assertNotIsSubclass to show that tuple is a child of object
42 def test_is_a_tuple_an_object(self): 43 # assert not issubclass(tuple, object) 44 assert issubclass(tuple, object) 45 self.assertNotIsSubclass(tuple, object) 46 47 48# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <class 'tuple'> is a subclass of <class 'object'>because tuple is a child of object.
I change assertNotIsSubclass to assertIsSubclass
42 def test_is_a_tuple_an_object(self): 43 # assert not issubclass(tuple, object) 44 assert issubclass(tuple, object) 45 # self.assertNotIsSubclass(tuple, object) 46 self.assertIsSubclass(tuple, object) 47 48 49# Exceptions seenthe test passes.
I remove the commented lines
42 def test_is_a_tuple_an_object(self): 43 assert issubclass(tuple, object) 44 self.assertIsSubclass(tuple, object) 45 46 47# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_is_a_tuple_an_object'
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 that is running the tests
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
42 def test_is_a_tuple_an_object(self): 43 assert issubclass(tuple, object) 44 self.assertIsSubclass(tuple, object) 45 46 def test_is_a_list_an_object(self): 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
46 def test_is_a_list_an_object(self):
47 # assert not issubclass(list, object)
48 assert issubclass(list, object)
49
50
51 # Exceptions seen
the test passes.
REFACTOR: make it better
I use assertNotIsSubclass to show that list is a child of object
46 def test_is_a_list_an_object(self): 47 # assert not issubclass(list, object) 48 assert issubclass(list, object) 49 self.assertNotIsSubclass(list, object) 50 51 52# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <class 'list'> is a subclass of <class 'object'>because list is a child of object.
I change assertNotIsSubclass to assertIsSubclass
46 def test_is_a_list_an_object(self): 47 # assert not issubclass(list, object) 48 assert issubclass(list, object) 49 # self.assertNotIsSubclass(list, object) 50 self.assertIsSubclass(list, object) 51 52 53# Exceptions seenthe test passes.
I remove the commented lines
46 def test_is_a_list_an_object(self): 47 assert issubclass(list, object) 48 self.assertIsSubclass(list, object) 49 50 51# Exceptions seenI add a git commit message in the other terminal
git commit -am 'add test_is_a_list_an_object'
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 that is running the tests
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 object46 def test_is_a_list_an_object(self): 47 assert issubclass(list, object) 48 self.assertIsSubclass(list, object) 49 50 def test_is_a_set_an_object(self): 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
50 def test_is_a_set_an_object(self):
51 # assert not issubclass(set, object)
52 assert issubclass(set, object)
53
54
55# Exceptions seen
the test passes.
REFACTOR: make it better
I use assertNotIsSubclass to show that set is a child of object
50 def test_is_a_set_an_object(self): 51 # assert not issubclass(set, object) 52 assert issubclass(set, object) 53 self.assertNotIsSubclass(set, object) 54 55 56# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <class 'set'> is a subclass of <class 'object'>because set is a child of object.
I change assertNotIsSubclass to assertIsSubclass
50 def test_is_a_set_an_object(self): 51 # assert not issubclass(set, object) 52 assert issubclass(set, object) 53 # self.assertNotIsSubclass(set, object) 54 self.assertIsSubclass(set, object) 55 56 57# Exceptions seenthe test passes.
I remove the commented lines
50 def test_is_a_set_an_object(self): 51 assert issubclass(set, object) 52 self.assertIsSubclass(set, object) 53 54 55# Exceptions seenI add a git commit message in the other terminal
git commit -am 'add test_is_a_set_an_object'
test_is_a_dictionary_an_object
I want to test if a dictionary (any key-value pairs in curly braces { } separated by a comma) is an object.
RED: make it fail
I go back to the terminal that is running the tests
I add a test with an assertion for dict (the class for key-value pairs in curly braces ‘{ }’ separated by a comma), to show that in Python everything is an object
50 def test_is_a_set_an_object(self): 51 assert issubclass(set, object) 52 self.assertIsSubclass(set, object) 53 54 def test_is_a_dictionary_an_object(self): 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.
I change the statement to make it True
54 def test_is_a_dictionary_an_object(self): 55 # assert not issubclass(dict, object) 56 assert issubclass(dict, object) 57 58 59# Exceptions seenthe test passes.
I use assertNotIsSubclass to show that dict is a child of object
54 def test_is_a_dictionary_an_object(self): 55 # assert not issubclass(dict, object) 56 assert issubclass(dict, object) 57 self.assertNotIsSubclass(dict, object) 58 59 60# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <class 'dict'> is a subclass of <class 'object'>because dict is a child of object.
I change assertNotIsSubclass to assertIsSubclass
54 def test_is_a_dictionary_an_object(self): 55 # assert not issubclass(dict, object) 56 assert issubclass(dict, object) 57 # self.assertNotIsSubclass(dict, object) 58 self.assertIsSubclass(dict, object) 59 60 61# Exceptions seenthe test passes.
I remove the commented lines
54 def test_is_a_dictionary_an_object(self): 55 assert issubclass(dict, object) 56 self.assertIsSubclass(dict, object) 57 58 59# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_is_a_dictionary_an_object'
instance vs subclass
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)
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 assert issubclass(an_instance, object) 12 13 def test_making_a_class_w_parentheses(self):the terminal is my friend, and shows TypeError
TypeError: issubclass() arg 1 must be a classbecause the argument I put on the left is an instance not a subclass.
I add TypeError to the list of Exceptions seen
60# Exceptions seen 61# AssertionError 62# NameError 63# AttributeError 64# TypeError
GREEN: make it pass
I change the assertion to make the statement True
7 def test_making_a_class_w_pass(self):
8 an_instance = src.classes.WPass()
9 assert isinstance(an_instance, object)
10 self.assertIsInstance(an_instance, object)
11 # assert issubclass(an_instance, object)
12 assert issubclass(src.classes.WPass, object)
13
14 def test_making_a_class_w_parentheses(self):
the test passes.
REFACTOR: make it better
I add a call to the assertNotIsSubclass method
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 # assert issubclass(an_instance, object) 12 assert issubclass(src.classes.WPass, object) 13 self.assertNotIsSubclass( 14 src.classes.WPass, object 15 ) 16 17 def test_making_a_class_w_parentheses(self):the terminal is my friend, and shows AssertionError
AssertionError: <class 'src.classes.WPass'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 # assert issubclass(an_instance, object) 12 assert issubclass(src.classes.WPass, object) 13 # self.assertNotIsSubclass( 14 self.assertIsSubclass( 15 src.classes.WPass, object 16 ) 17 18 def test_making_a_class_w_parentheses(self):the test passes.
I add a variable
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 12 a_class = src.classes.WPass 13 # assert issubclass(an_instance, object) 14 assert issubclass(src.classes.WPass, object) 15 # self.assertNotIsSubclass( 16 self.assertIsSubclass( 17 src.classes.WPass, object 18 ) 19 20 def test_making_a_class_w_parentheses(self):I use the variable to remove repetition of
src.classes.WPass7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 12 a_class = src.classes.WPass 13 assert issubclass(a_class, object) 14 self.assertIsSubclass(a_class, object) 15 # assert issubclass(an_instance, object) 16 # assert issubclass(src.classes.WPass, object) 17 # self.assertNotIsSubclass( 18 # self.assertIsSubclass( 19 # src.classes.WPass, object 20 # ) 21 22 def test_making_a_class_w_parentheses(self):the test is still green.
I remove the commented lines
7 def test_making_a_class_w_pass(self): 8 an_instance = src.classes.WPass() 9 assert isinstance(an_instance, object) 10 self.assertIsInstance(an_instance, object) 11 12 a_class = src.classes.WPass 13 assert issubclass(a_class, object) 14 self.assertIsSubclass(a_class, object) 15 16 def test_making_a_class_w_parentheses(self):
I add an assertion to test_making_a_class_w_parentheses
16 def test_making_a_class_w_parentheses(self): 17 an_instance = src.classes.WParentheses() 18 assert isinstance(an_instance, object) 19 self.assertIsInstance(an_instance, object) 20 21 assert issubclass(an_instance, object) 22 23 def test_making_a_class_w_object(self):the terminal is my friend, and shows TypeError
TypeError: issubclass() arg 1 must be a classbecause
src.classes.WParentheses()is an instance not a subclass.I change the assertion to make the statement True
16 def test_making_a_class_w_parentheses(self): 17 an_instance = src.classes.WParentheses() 18 assert isinstance(an_instance, object) 19 self.assertIsInstance(an_instance, object) 20 21 # assert issubclass(an_instance, object) 22 assert issubclass( 23 src.classes.WParentheses, object 24 ) 25 26 def test_making_a_class_w_object(self):the test passes.
I add a call to the assertNotIsSubclass method
16 def test_making_a_class_w_parentheses(self): 17 an_instance = src.classes.WParentheses() 18 assert isinstance(an_instance, object) 19 self.assertIsInstance(an_instance, object) 20 21 # assert issubclass(an_instance, object) 22 assert issubclass( 23 src.classes.WParentheses, object 24 ) 25 self.assertNotIsSubclass( 26 src.classes.WParentheses, object 27 ) 28 29 def test_making_a_class_w_object(self):the terminal is my friend, and shows AssertionError
AssertionError: <class 'src.classes.WParentheses'> is a subclass of <class 'object'>I change assertNotIsSubclass to the assertIsSubclass method
16 def test_making_a_class_w_parentheses(self): 17 an_instance = src.classes.WParentheses() 18 assert isinstance(an_instance, object) 19 self.assertIsInstance(an_instance, object) 20 21 # assert issubclass(an_instance, object) 22 assert issubclass( 23 src.classes.WParentheses, object 24 ) 25 # self.assertNotIsSubclass( 26 self.assertIsSubclass( 27 src.classes.WParentheses, object 28 ) 29 30 def test_making_a_class_w_object(self):the test passes.
I add a variable
16 def test_making_a_class_w_parentheses(self): 17 an_instance = src.classes.WParentheses() 18 assert isinstance(an_instance, object) 19 self.assertIsInstance(an_instance, object) 20 21 a_class = src.classes.WParentheses 22 # assert issubclass(an_instance, object) 23 assert issubclass( 24 src.classes.WParentheses, object 25 ) 26 # self.assertNotIsSubclass( 27 self.assertIsSubclass( 28 src.classes.WParentheses, object 29 ) 30 31 def test_making_a_class_w_object(self):I use the variable to remove repetition of
src.classes.WParentheses16 def test_making_a_class_w_parentheses(self): 17 an_instance = src.classes.WParentheses() 18 assert isinstance(an_instance, object) 19 self.assertIsInstance(an_instance, object) 20 21 a_class = src.classes.WParentheses 22 assert issubclass(a_class, object) 23 self.assertIsSubclass(a_class, object) 24 # assert issubclass(an_instance, object) 25 # assert issubclass( 26 # src.classes.WParentheses, object 27 # ) 28 # self.assertNotIsSubclass( 29 # self.assertIsSubclass( 30 # src.classes.WParentheses, object 31 # ) 32 33 def test_making_a_class_w_object(self):the test is still green.
I remove the commented lines
16 def test_making_a_class_w_parentheses(self): 17 an_instance = src.classes.WParentheses() 18 assert isinstance(an_instance, object) 19 self.assertIsInstance(an_instance, object) 20 21 a_class = src.classes.WParentheses 22 assert issubclass(a_class, object) 23 self.assertIsSubclass(a_class, object) 24 25 def test_making_a_class_w_object(self):
I also add an assertion to test_making_a_class_w_object
25 def test_making_a_class_w_object(self): 26 an_instance = src.classes.WObject() 27 assert isinstance(an_instance, object) 28 self.assertIsInstance(an_instance, object) 29 30 assert issubclass(an_instance, object) 31 32 def test_is_none_an_object(self):the terminal is my friend, and shows TypeError
TypeError: issubclass() arg 1 must be a classbecause
src.classes.WObject()is an instance not a subclass.I change the assertion to make the statement True
25 def test_making_a_class_w_object(self): 26 an_instance = src.classes.WObject() 27 assert isinstance(an_instance, object) 28 self.assertIsInstance(an_instance, object) 29 30 # assert issubclass(an_instance, object) 31 assert issubclass( 32 src.classes.WObject, object 33 ) 34 35 def test_is_none_an_object(self):the test passes.
I add a call to the assertNotIsSubclass method
25 def test_making_a_class_w_object(self): 26 an_instance = src.classes.WObject() 27 assert isinstance(an_instance, object) 28 self.assertIsInstance(an_instance, object) 29 30 # assert issubclass(an_instance, object) 31 assert issubclass( 32 src.classes.WObject, object 33 ) 34 self.assertNotIsSubclass( 35 src.classes.WObject, object 36 ) 37 38 def test_is_none_an_object(self):the terminal is my friend, and shows AssertionError
AssertionError: <class 'src.classes.WObject'> is a subclass of <class 'object'>I change assertNotIsSubclass to the assertIsSubclass method
25 def test_making_a_class_w_object(self): 26 an_instance = src.classes.WObject() 27 assert isinstance(an_instance, object) 28 self.assertIsInstance(an_instance, object) 29 30 # assert issubclass(an_instance, object) 31 assert issubclass( 32 src.classes.WObject, object 33 ) 34 # self.assertNotIsSubclass( 35 self.assertIsSubclass( 36 src.classes.WObject, object 37 ) 38 39 def test_is_none_an_object(self):the test passes.
I add a variable
25 def test_making_a_class_w_object(self): 26 an_instance = src.classes.WObject() 27 assert isinstance(an_instance, object) 28 self.assertIsInstance(an_instance, object) 29 30 a_class = src.classes.WObject 31 # assert issubclass(an_instance, object) 32 assert issubclass( 33 src.classes.WObject, object 34 ) 35 # self.assertNotIsSubclass( 36 self.assertIsSubclass( 37 src.classes.WObject, object 38 ) 39 40 def test_is_none_an_object(self):I use the variable to remove repetition of
src.classes.WObject25 def test_making_a_class_w_object(self): 26 an_instance = src.classes.WObject() 27 assert isinstance(an_instance, object) 28 self.assertIsInstance(an_instance, object) 29 30 a_class = src.classes.WObject 31 assert issubclass(a_class, object) 32 self.assertIsSubclass(a_class, object) 33 # assert issubclass(an_instance, object) 34 # assert issubclass( 35 # src.classes.WObject, object 36 # ) 37 # self.assertNotIsSubclass( 38 # self.assertIsSubclass( 39 # src.classes.WObject, object 40 # ) 41 42 def test_is_none_an_object(self):the test is still green.
I remove the commented lines
25 def test_making_a_class_w_object(self): 26 an_instance = src.classes.WObject() 27 assert isinstance(an_instance, object) 28 self.assertIsInstance(an_instance, object) 29 30 a_class = src.classes.WObject 31 assert issubclass(a_class, object) 32 self.assertIsSubclass(a_class, object) 33 34 def test_is_none_an_object(self):I add a git commit message in the other terminal
git commit -am 'add instance vs subclass'
The difference between a subclass (child) 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()
test_attributes_and_methods_of_objects
In test_attributes_and_methods_of_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 that is running the tests
I add a test to
test_classes.py66 def test_is_a_dictionary_an_object(self): 67 assert issubclass(dict, object) 68 self.assertIsSubclass(dict, object) 69 70 def test_attributes_and_methods_of_objects(self): 71 reality = dir(object) 72 my_expectation = [] 73 self.assertEqual(reality, my_expectation) 74 75 76# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: Lists differ: ['__class__', '__delattr__', '__dir__', '_[272 chars]k__'] != []
GREEN: make it pass
I copy (ctrl/command+c) the values from the terminal and paste (ctrl/command+v) them as
my_expectation70 def test_attributes_and_methods_of_objects(self): 71 reality = dir(object) 72 # my_expectation = [] 73 my_expectation = [ 74 '__class__', '__delattr__', '__dir__', 75 '_[272 chars]k__' 76 ] 77 self.assertEqual(reality, my_expectation) 78 79 80# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: Lists differ: ['__c[32 chars]', '__doc__', '__eq__', '__format__', '__ge__'[231 chars]k__'] != ['__c[32 chars]', '_[272 chars]k__']it shows me the entire list below the message
I copy (ctrl/command+c) the values from the terminal and paste (ctrl/command+v) them as
my_expectation70 def test_attributes_and_methods_of_objects(self): 71 reality = dir(object) 72 # my_expectation = [] 73 # my_expectation = [ 74 # '__class__', '__delattr__', '__dir__', 75 # '_[272 chars]k__' 76 # ] 77 my_expectation = E - ['__class__', 78E - '__delattr__', 79E - '__dir__', 80E - '__doc__', 81E - '__eq__', 82E - '__format__', 83E - '__ge__', 84E - '__getattribute__', 85E - '__getstate__', 86E - '__gt__', 87E - '__hash__', 88E - '__init__', 89E - '__init_subclass__', 90E - '__le__', 91E - '__lt__', 92E - '__ne__', 93E - '__new__', 94E - '__reduce__', 95E - '__reduce_ex__', 96E - '__repr__', 97E - '__setattr__', 98E - '__sizeof__', 99E - '__str__', 100E - '__subclasshook__'] 101 self.assertEqual(reality, my_expectation) 102 103 104# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'E' is not definedI use the
find and replacefeature of the Integrated Development Environment (IDE) to remove the extra characters, then remove the commented lines70 def test_attributes_and_methods_of_objects(self): 71 reality = dir(object) 72 my_expectation = [ 73 '__class__', 74 '__delattr__', 75 '__dir__', 76 '__doc__', 77 '__eq__', 78 '__format__', 79 '__ge__', 80 '__getattribute__', 81 '__getstate__', 82 '__gt__', 83 '__hash__', 84 '__init__', 85 '__init_subclass__', 86 '__le__', 87 '__lt__', 88 '__ne__', 89 '__new__', 90 '__reduce__', 91 '__reduce_ex__', 92 '__repr__', 93 '__setattr__', 94 '__sizeof__', 95 '__str__', 96 '__subclasshook__' 97 ] 98 self.assertEqual(reality, my_expectation) 99 100 101# Exceptions seenthe test passes. 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_attributes_and_methods_of_objects'
review
I can make a class with
Everything in Python is an object
close the project
I close
test_classes.pyandclasses.pyin the editor(s)I 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
personcd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory
code from the chapter
what is next?
You now know
Would you like to use class attributes to remove repetition from the assertion_error project?
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.