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?

A method is a function that belongs to a class.


how to make a class

classes are made with

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 classes

  • I open a terminal

  • I change directory to the classes folder in the pumping_python folder

    cd classes
    

    the terminal shows

    cd: no such file or directory: classes
    
  • I use uv to make a directory for the project and initialize it

    uv init classes
    

    the terminal shows

    Initialized project `classes`
    at `.../pumping_python/classes`
    
  • I change directory to classes

    cd classes
    

    the terminal shows I am in the classes folder

    .../pumping_python/classes
    
  • I make a directory for the tests

    mkdir tests
    
  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    
    New-Item tests/__init__.py
    
  • I use the mv program to change the name of main.py to test_classes.py and move it to the tests folder

    mv main.py tests/test_classes.py
    
    Move-Item main.py tests/test_classes.py
    
  • I open test_classes.py

  • I delete the text in the file then add the first failing test to test_classes.py

    1def test_failure():
    2    assert False is True
    
  • I go back to the terminal to make a requirements file for the Python packages I need

    echo "pytest" > requirements.txt
    
  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    
  • I use uv to install pytest-watcher with the requirements file

    uv add --requirement requirements.txt
    
  • I 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-watcher to run the tests

    uv run pytest-watcher . --now
    

    the 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 ====================
    

    because False is NOT True.

    if the terminal does not show the same error, then check if

    • your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    and try uv run pytest-watcher . --now again

  • I add AssertionError to the list of Exceptions seen

    1def test_failure():
    2    assert False is True
    3
    4
    5# Exceptions seen
    6# AssertionError
    
  • I 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# AssertionError
    

    the 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



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.


REFACTOR: make it better


  • I remove the commented line

    4def test_making_a_class_w_pass():
    5    assert isinstance(WPass(), object)
    6
    7
    8# Exceptions seen
    
  • I open a new terminal then make sure I am in the classes folder

    cd classes
    
  • I add a git commit message in the new terminal

    git commit -am \
    'add test_making_a_class_w_pass'
    

I can make a class with 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 where the tests are running

  • I add another test to test_classes.py

     4def 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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'WParentheses' is not defined
    

GREEN: make it pass


  • I add a class definition for WParentheses like I did for WPass

    1class 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 True
    

    because 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 seen
    

    the test passes.


REFACTOR: make it better


  • I add parentheses to the definition of WParentheses

    4# class WParentheses: pass
    5class WParentheses(): pass
    6
    7
    8def test_making_a_class_w_pass():
    
  • 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 seen
    
  • I 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.py

    11def 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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'WObject' is not defined
    

GREEN: make it pass


  • I add a class definition for WObject

     4class 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 True
    

    because 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 seen
    

    the test passes.


REFACTOR: make it better


  • I add object to the parentheses of the class definition for WObject

     7# 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 seen
    
  • I 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.

I can make a class with object.


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 seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because None is an object.


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


  • I remove the commented line

    22def test_is_none_a_object():
    23    assert isinstance(None, object)
    24
    25
    26# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_a_object'
    

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



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


  • I remove the commented line

    26def test_is_a_boolean_an_object():
    27    assert issubclass(bool, object)
    28
    29
    30# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_boolean_an_object'
    

A boolean is 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 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 seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because 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


  • I remove the commented line

    30def test_is_an_integer_an_object():
    31    assert issubclass(int, object)
    32
    33
    34# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_an_integer_an_object'
    

An integer is 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 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 seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because 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


  • I remove the commented line

    34def test_is_a_float_an_object():
    35    assert issubclass(float, object)
    36
    37
    38# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_float_an_object'
    

A float is 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



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


  • I remove the commented line

    38def test_is_a_string_an_object():
    39    assert issubclass(str, object)
    40
    41
    42# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_string_an_object'
    

A string is 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



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


  • I remove the commented line

    42def test_is_a_tuple_an_object():
    43    assert issubclass(tuple, object)
    44
    45
    46# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_tuple_an_object'
    

A tuple is 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



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


  • I remove the commented line

    46def test_is_a_list_an_object():
    47    assert issubclass(list, object)
    48
    49
    50# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_a_list_an_object'
    

A list is 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



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


  • I remove the commented lines

    50def test_is_a_set_an_object():
    51    assert issubclass(set, object)
    52
    53
    54# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_a_set_an_object'
    

A set is an object.


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



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


  • I remove the commented line

    54def test_is_a_dictionary_an_object():
    55    assert issubclass(dict, object)
    56
    57
    58# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_dictionary_an_object'
    

A dictionary is an object.


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



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 class
    

    because 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 class
    

    because WObject() is an instance not a subclass.

  • I 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 class
    

    because None is not a class.

  • I 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 True
    

    because 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 True
    

    because 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 True
    

    because 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 True
    

    because 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 True
    

    because 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 True
    

    because 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 True
    

    because 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 seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because 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 seen
    

    the 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 seen
    
  • I 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.py with a call to the dir built-in function to get the attributes and methods of object

    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    assert reality == my_expectation
    75
    76
    77# Exceptions seen
    

    the 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 reality and my_expectation and it shows AssertionError

    E         ...Full output truncated (24 lines hidden),
                 use '-vv' to show
    
  • I 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-watcher
    
  • I 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: -vv
    
  • I 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_expectation

    71def 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 seen
    
  • 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

How many questions can you answer about classes?


close the project

  • I close test_classes.py

  • 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 classes

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


code from the chapter

Do you want to see all the CODE I typed in this 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.