None
what is None?
None is used when there is no value. It is the simplest data structure in Python
I use assert methods to compare None with the other Python data structures to see what it is and what it is not
preview
In AssertionError, I used assertIsNone and assertIsNotNone to test_assertion_error_w_none, that experiment showed that
Here are the tests I have by the end of the chapter
1import unittest
2
3
4class TestNone(unittest.TestCase):
5
6 def test_what_is_none(self):
7 self.assertIsNone(None)
8
9 def test_is_none_a_boolean(self):
10 self.assertIsNotNone(False)
11 self.assertIsNotNone(True)
12 self.assertIsInstance(False, bool)
13 self.assertIsInstance(True, bool)
14 self.assertNotIsInstance(None, bool)
15
16 def test_is_none_an_integer(self):
17 self.assertIsNotNone(-1)
18 self.assertIsNotNone(0)
19 self.assertIsNotNone(1)
20 self.assertIsInstance(-1, int)
21 self.assertIsInstance(0, int)
22 self.assertIsInstance(1, int)
23 self.assertNotIsInstance(None, int)
24
25 def test_is_none_a_float(self):
26 self.assertIsNotNone(-0.1)
27 self.assertIsNotNone(0.0)
28 self.assertIsNotNone(0.1)
29 self.assertIsInstance(-0.1, float)
30 self.assertIsInstance(0.0, float)
31 self.assertIsInstance(0.1, float)
32 self.assertNotIsInstance(None, float)
33
34 def test_is_none_a_string(self):
35 self.assertIsNotNone('')
36 self.assertIsNotNone("text")
37 self.assertIsInstance('', str)
38 self.assertIsInstance("text", str)
39 self.assertNotIsInstance(None, str)
40
41 def test_is_none_a_tuple(self):
42 self.assertIsNotNone(())
43 self.assertIsNotNone((1, 2, 3, 'n'))
44 self.assertIsInstance((), tuple)
45 self.assertIsInstance((1, 2, 3, 'n'), tuple)
46 self.assertNotIsInstance(None, tuple)
47
48 def test_is_none_a_list(self):
49 self.assertIsNotNone([])
50 self.assertIsNotNone([1, 2, 3, 'n'])
51 self.assertIsInstance([], list)
52 self.assertIsInstance([1, 2, 3, 'n'], list)
53 self.assertNotIsInstance(None, list)
54
55 def test_is_none_a_set(self):
56 self.assertIsNotNone(set())
57 self.assertIsNotNone({1, 2, 3, 'n'})
58 self.assertIsInstance({1, 2, 3, 'n'}, set)
59 self.assertNotIsInstance(None, set)
60
61 def test_is_none_a_dictionary(self):
62 self.assertIsNotNone(dict())
63 self.assertIsNotNone({'key': 'value'})
64 self.assertIsInstance({}, dict)
65 self.assertIsInstance({'key': 'value'}, dict)
66 self.assertNotIsInstance(None, dict)
67
68
69# NOTES
70# None is NOT a dictionary
71# None is NOT a set
72# None is NOT a list
73# None is NOT a tuple
74# None is NOT a string
75# None is NOT a float
76# None is NOT an integer
77# None is NOT a boolean
78# None is None
79
80
81# Exceptions seen
82# AssertionError
questions about None
Here are questions you can answer after going through this chapter
requirements
I name this project
noneI open a terminal
then I make a directory for the project
mkdir nonethe terminal goes back to the command line
.../pumping_pythonI change directory to the project
cd nonethe terminal shows I am now in the
nonefolder.../pumping_python/noneI make a folder for the source code
mkdir srcthe terminal goes back to the command line
.../pumping_python/noneI use touch to make an empty file for the program in the
srcfoldertouch src/none.pyon Windows without Windows Subsystem for Linux use
New-Item src/none.pyinstead oftouch src/none.pyNew-Item src/none.pythe terminal goes back to the command line
.../pumping_python/noneI make a directory for the tests
mkdir teststhe terminal goes back to the command line
I use touch to make an empty file in the
testsfolder to tell Python that it is a Python packageAttention
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyon Windows without Windows Subsystem for Linux use
New-Item tests/__init__.pyinstead oftouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line
I make an empty file for the actual test
touch tests/test_none.pyon Windows without Windows Subsystem for Linux use
New-Item tests/test_none.pyinstead oftouch tests/test_none.pyNew-Item tests/test_none.pythe terminal goes back to the command line
I open
test_none.pyin the editor of the Integrated Development Environment (IDE)Tip
I can open a file from the terminal in Visual Studio Code by typing
codeand the name of the file withcode tests/test_none.pytest_none.pyopens up in the editorI add the first failing test to
test_none.py1import unittest 2 3 4class TestNone(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True)I make a virtual environment in the terminal
python3 -m venv .venvon Windows without Windows Subsystem for Linux use
python3 -m venv .venvinstead ofpython3 -m venv .venvpython -m venv .venvthe terminal takes some time then goes back to the command line
I activate the virtual environment
source .venv/bin/activateon Windows without Windows Subsystem for Linux use
.venv/bin/activate.ps1instead ofsource .venv/bin/activate.venv/scripts/activate.ps1the terminal shows
(.venv) .../pumping_python/noneI upgrade the Python package manager (pip) to the latest version
python3 -m pip install --upgrade pipthe terminal shows pip being uninstalled then installs the latest version or shows that it is already the latest version
I make a
requirements.txtfile for the Python programs my project needsecho "pytest-watch" > requirements.txtthe terminal goes back to the command line
I use pip to use the requirements file to install
pytest-watchpython3 -m pip install --requirement requirements.txton Windows without Windows Subsystem for Linux use
python -m pip install --requirement requirements.txtinstead ofpython3 -m pip install --requirement requirements.txtpython -m pip install --requirement requirements.txtthe terminal shows pip downloads and installs the Python programs that pytest-watch needs to run
I use pytest-watch to run the test
pytest-watchthe terminal shows
================================ FAILURES ================================ _________________________ TestNone.test_failure __________________________ self = <tests.test_none.TestNone testMethod=test_failure> def test_failure(self): > self.assertFalse(True) E AssertionError: True is not false tests/test_none.py:7: AssertionError ======================== short test summary info ========================= FAILED tests/test_none.py::TestNone::test_failure - AssertionError: True is not false =========================== 1 failed in X.YZs ============================I hold ctrl (Windows/Linux) or
option or command(MacOS) on the keyboard and use the mouse to click ontests/test_none.py:7to open it in the editorI add AssertionError to the list of Exceptions seen in
test_none.py7 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_what_is_none
We need a way to represent nothing or the absence of a value. In mathematics we use 0 to represent no quantity. In some languages or domains we use null, in forms we use N/A when the options do not apply. In Python we use None
RED: make it fail
I change
test_failuretotest_what_is_none4class TestNone(unittest.TestCase): 5 6 def test_what_is_none(self): 7 self.assertIsNotNone(None)the terminal shows AssertionError
AssertionError: unexpectedly NoneassertIsNotNone checks that what is in the parentheses is NOT None
GREEN: make it pass
I change the assertIsNotNone to assertIsNone, which checks if what it gets in parentheses is None
7 self.assertIsNone(None)
the test passes
REFACTOR: make it better
I add a note
10# NOTES
11# None is None
12
13
14# Exceptions seen
15# AssertionError
so far this is a repetition of AssertionError
test_is_none_a_boolean
RED: make it fail
I add another failing test to see if None is a boolean
7 self.assertIsNone(None)
8
9 def test_is_none_a_boolean(self):
10 self.assertIsNone(False)
11
12
13# NOTES
the terminal shows AssertionError
AssertionError: False is not None
GREEN: make it pass
I make the assertion True with the assertIsNotNone method
10 self.assertIsNotNone(False)
the test passes
REFACTOR: make it better
I add a note
13# NOTES 14# False is NOT None 15# None is NoneStill a repetition of what I learned in AssertionError
I add another failing assertion for the other boolean
10 self.assertIsNotNone(False) 11 self.assertIsNone(True)the terminal shows AssertionError
AssertionError: True is not NoneI make the assertion True with assertIsNotNone
11 self.assertIsNotNone(True)the test passes
I add a note
14# NOTES 15# True is NOT None 16# False is NOT None 17# None is Nonemore repetition
how to test if something is an instance of a class
The unittest.TestCase class has 2 methods I can use to test if an object is a child/instance of a class or not - assertIsInstance and assertNotIsInstance
I add the assertNotIsInstance method to test if False is a boolean
11 self.assertIsNotNone(True) 12 self.assertNotIsInstance(False, bool)assertNotIsInstance checks if the first item it is given is NOT a child/instance of the second item. It is like asking the question
is False NOT a child of the bool class?. Okay, this is new
the terminal shows AssertionError
AssertionError: False is an instance of <class 'bool'>I make the assertion True with the assertIsInstance method which checks if the first item it is given is a child/instance of the second item. It is like asking the question
is False a child of the bool class?12 self.assertIsInstance(False, bool)the test passes
I add a failing line for the other boolean with assertNotIsInstance
12 self.assertIsInstance(False, bool) 13 self.assertNotIsInstance(True, bool)the terminal shows AssertionError
AssertionError: True is an instance of <class 'bool'>I make the statement True with assertIsInstance
13 self.assertIsInstance(True, bool)the test passes
I add assertIsInstance to test if None is a child/instance of the bool class
13 self.assertIsInstance(True, bool) 14 self.assertIsInstance(None, bool)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'bool'>I make the line True with assertNotIsInstance
9 def test_is_none_a_boolean(self): 10 self.assertIsNotNone(False) 11 self.assertIsNotNone(True) 12 self.assertIsInstance(False, bool) 13 self.assertIsInstance(True, bool) 14 self.assertNotIsInstance(None, bool) 15 16 17# NOTESthe test passes
Since this is about None, I change the last 2 notes I added
17# NOTES 18# None is NOT a boolean 19# None is None
I know two new assert methods
assertIsInstance to see if something is an instance of a class
assertNotIsInstance to see if something is NOT an instance of a class
Caution
the naming of the assert methods can be confusing, there is
where Not comes after Is and then there is
where Is comes after Not. Maybe assertIsNotInstance would have been better, since assertNotIsNone does not sound better than assertIsNotNone. Naming things is its own challenge
test_is_none_an_integer
RED: make it fail
I add a test to see if None is an integer (a whole number)
14 self.assertNotIsInstance(None, bool)
15
16 def test_is_none_an_integer(self):
17 self.assertIsNone(-1)
the terminal shows AssertionError
AssertionError: -1 is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
17 self.assertIsNotNone(-1)
the test passes
REFACTOR: make it better
I add a new failing line with assertIsNone
17 self.assertIsNotNone(-1) 18 self.assertIsNone(0)the terminal shows AssertionError
AssertionError: 0 is not NoneI make the statement True with assertIsNotNone
18 self.assertIsNotNone(0)the test passes
I add another assertion
18 self.assertIsNotNone(0) 19 self.assertIsNone(1)the terminal shows AssertionError
AssertionError: 1 is not NoneI change the line to make it True
19 self.assertIsNotNone(1)the test passes
I add a new failing line with assertNotIsInstance
19 self.assertIsNotNone(1) 20 self.assertNotIsInstance(-1, int)int is the class for integers, the terminal shows AssertionError
AssertionError: -1 is an instance of <class 'int'>I use
-1for all the integers (whole numbers) that are smaller than0I make the line True with assertIsInstance
20 self.assertIsInstance(-1, int)the test passes
I add another failing line with assertNotIsInstance
20 self.assertIsInstance(-1, int) 21 self.assertNotIsInstance(0, int)the terminal shows AssertionError
AssertionError: 0 is an instance of <class 'int'>0is an integerI change the statement to make it True
21 self.assertIsInstance(0, int)the test passes
I add another failing line with assertNotIsInstance
21 self.assertIsInstance(0, int) 22 self.assertNotIsInstance(1, int)the terminal shows AssertionError
AssertionError: 1 is an instance of <class 'int'>I use
1for all the integers (whole numbers) that are bigger than0I make the failing line True with assertIsInstance
22 self.assertIsInstance(1, int)the test passes
I add one more failing line with to test if None is an integer with assertIsInstance
22 self.assertIsInstance(1, int) 23 self.assertIsInstance(None, int)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'int'>I make the line True with assertNotIsInstance
16 def test_is_none_an_integer(self): 17 self.assertIsNotNone(-1) 18 self.assertIsNotNone(0) 19 self.assertIsNotNone(1) 20 self.assertIsInstance(-1, int) 21 self.assertIsInstance(0, int) 22 self.assertIsInstance(1, int) 23 self.assertNotIsInstance(None, int) 24 25 26# NOTESthe test passes
I add a new note
26# NOTES 27# None is NOT an integer 28# None is NOT a boolean 29# None is None
test_is_none_a_float
RED: make it fail
I add a test to see if None is a float (floating point decimal number)
23 self.assertNotIsInstance(None, int)
24
25 def test_is_none_a_float(self):
26 self.assertIsNone(-0.1)
27
28
29# NOTES
the terminal shows AssertionError
AssertionError: -0.1 is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
26 self.assertIsNotNone(-0.1)
the test passes
REFACTOR: make it better
I add another failing line with assertIsNone
26 self.assertIsNotNone(-0.1) 27 self.assertIsNone(0.0)the terminal shows AssertionError
AssertionError: 0.0 is not NoneI make the statement True with assertIsNotNone
27 self.assertIsNotNone(0.0)the test passes
I add a failing line with assertIsNone
27 self.assertIsNotNone(0.0) 28 self.assertIsNone(0.1)the terminal shows AssertionError
AssertionError: 0.1 is not NoneI make the statement True with assertIsNotNone
28 self.assertIsNotNone(0.1)the test passes
time for instance tests. I add a failing line with assertNotIsInstance
28 self.assertIsNotNone(0.1) 29 self.assertNotIsInstance(-0.1, float)float is the class for floating point numbers. The terminal shows AssertionError
AssertionError: -0.1 is an instance of <class 'float'>I use
-0.1for all the floating point numbers that are smaller than0.0I make the statement True with assertIsInstance
29 self.assertIsInstance(-0.1, float)the test passes
I add the next instance test with assertNotIsInstance
29 self.assertIsInstance(-0.1, float) 30 self.assertNotIsInstance(0.0, float)the terminal shows AssertionError
AssertionError: 0.0 is an instance of <class 'float'>0.0is a floating point numberI make the statement True with assertIsInstance
30 self.assertIsInstance(0.0, float)the test passes
I add a failing line with assertNotIsInstance
30 self.assertIsInstance(0.0, float) 31 self.assertNotIsInstance(0.1, float)the terminal shows AssertionError
AssertionError: 0.1 is an instance of <class 'float'>I use
0.1for all the floating point numbers that are bigger than0.0I make the statement True with assertIsInstance
31 self.assertIsInstance(0.1, float)the test passes
I add one more failing line with the assertIsInstance method
31 self.assertIsInstance(0.1, float) 32 self.assertIsInstance(None, float)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'float'>I make the statement True with the assertNotIsInstance method
25 def test_is_none_a_float(self): 26 self.assertIsNotNone(-0.1) 27 self.assertIsNotNone(0.0) 28 self.assertIsNotNone(0.1) 29 self.assertIsInstance(-0.1, float) 30 self.assertIsInstance(0.0, float) 31 self.assertIsInstance(0.1, float) 32 self.assertNotIsInstance(None, float) 33 34 35# NOTESthe test passes
I add a new note
35# NOTES 36# None is NOT a float 37# None is NOT an integer 38# None is NOT a boolean 39# None is None
test_is_none_a_string
RED: make it fail
I add a test to see if None is a string (anything inside quotes)
32 self.assertNotIsInstance(None, float)
33
34 def test_is_none_a_string(self):
35 self.assertIsNone('')
36
37
38# NOTES
the terminal shows AssertionError
AssertionError: '' is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
35 self.assertIsNotNone('')
the test passes
REFACTOR: make it better
I add another failing line with assertIsNone
35 self.assertIsNotNone('') 36 self.assertIsNone("text")the terminal shows AssertionError
AssertionError: 'text' is not NoneI change the assert method to make the statement True
36 self.assertIsNotNone("text")the test passes
I add a failing line with assertNotIsInstance
36 self.assertIsNotNone("text") 37 self.assertNotIsInstance('', str)str is the class for strings. The terminal shows AssertionError
AssertionError: '' is an instance of <class 'str'>I change the assert method
37 self.assertIsInstance('', str)the test passes
I add another assertion
37 self.assertIsInstance('', str) 38 self.assertNotIsInstance("text", str)the terminal shows AssertionError
AssertionError: 'text' is an instance of <class 'str'>I change the assert method
38 self.assertIsInstance("text", str)A string is anything in single, double or triple quotes, for example
'single quotes''''triple single quotes'''"double quotes""""triple double quotes"""
see quotes for more
I add another failing line with assertIsInstance
38 self.assertIsInstance("text", str) 39 self.assertIsInstance(None, str)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'str'>I change the assert method
34 def test_is_none_a_string(self): 35 self.assertIsNotNone('') 36 self.assertIsNotNone("text") 37 self.assertIsInstance('', str) 38 self.assertIsInstance("text", str) 39 self.assertNotIsInstance(None, str) 40 41 42# NOTESthe test passes
I add a note
42# NOTES 43# None is NOT a string 44# None is NOT a float 45# None is NOT an integer 46# None is NOT a boolean 47# None is None
test_is_none_a_tuple
RED: make it fail
I add a test to see if None is a tuple (anything in parentheses (())), pronounced two-pull
39 self.assertNotIsInstance(None, str)
40
41 def test_is_none_a_tuple(self):
42 self.assertIsNone(())
43
44
45# NOTES
the terminal shows AssertionError
AssertionError: () is not None
GREEN: make it pass
I make the statement True
42 self.assertIsNotNone(())
the test passes
REFACTOR: make it better
I add a failing assertion
42 self.assertIsNotNone(()) 43 self.assertIsNone((1, 2, 3, 'n'))the terminal shows AssertionError
AssertionError: (1, 2, 3, 'n') is not NoneI make the statement True
43 self.assertIsNotNone((1, 2, 3, 'n'))the test passes
I add a failing line with assertNotIsInstance
43 self.assertIsNotNone((1, 2, 3, 'n')) 44 self.assertNotIsInstance((), tuple)the terminal shows AssertionError
AssertionError: () is an instance of <class 'tuple'>I make the statement True
44 self.assertIsInstance((), tuple)the test passes
I add another failing line
44 self.assertIsInstance((), tuple) 45 self.assertNotIsInstance((1, 2, 3, 'n'), tuple)the terminal shows AssertionError
AssertionError: (1, 2, 3, 'n') is an instance of <class 'tuple'>I change the assert method to make the statement True
45 self.assertIsInstance((1, 2, 3, 'n'), tuple)the test passes
I add one more instance test
45 self.assertIsInstance((1, 2, 3, 'n'), tuple) 46 self.assertIsInstance(None, tuple)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'tuple'>I change the statement to make it True
41 def test_is_none_a_tuple(self): 42 self.assertIsNotNone(()) 43 self.assertIsNotNone((1, 2, 3, 'n')) 44 self.assertIsInstance((), tuple) 45 self.assertIsInstance((1, 2, 3, 'n'), tuple) 46 self.assertNotIsInstance(None, tuple) 47 48 49# NOTESthe test passes
I add a note
49# NOTES 50# None is NOT a tuple 51# None is NOT a string 52# None is NOT a float 53# None is NOT an integer 54# None is NOT a boolean 55# None is None
test_is_none_a_list
RED: make it fail
I add a new test to see if None is a list (anything in square brackets ([]))
46 self.assertNotIsInstance(None, tuple)
47
48 def test_is_none_a_list(self):
49 self.assertIsNone([])
50
51
52# NOTES
the terminal shows AssertionError
AssertionError: [] is not None
GREEN: make it pass
I change the assertion to make the statement True
49 self.assertIsNotNone([])
the test passes
REFACTOR: make it better
I add another failing line
49 self.assertIsNotNone([]) 50 self.assertIsNone([1, 2, 3, 'n'])the terminal shows AssertionError
AssertionError: [1, 2, 3, 'n'] is not NoneI make the statement True
50 self.assertIsNotNone([1, 2, 3, 'n'])the test passes
I add a failing instance test
50 self.assertIsNotNone([1, 2, 3, 'n']) 51 self.assertNotIsInstance([], list)the terminal shows AssertionError
AssertionError: [] is an instance of <class 'list'>because anything in square brackets (
[]) in Python is a listI make the statement True
51 self.assertIsInstance([], list)the test passes
I add another failing line with assertNotIsInstance
51 self.assertIsInstance([], list) 52 self.assertNotIsInstance([1, 2, 3, 'n'], list)the terminal shows AssertionError
AssertionError: [1, 2, 3, 'n'] is an instance of <class 'list'>because anything in square brackets (
[]) in Python is a listI change the assert method
52 self.assertIsInstance([1, 2, 3, 'n'], list)the test passes
I add one more failing line with the assertIsInstance method
52 self.assertIsInstance([1, 2, 3, 'n'], list) 53 self.assertIsInstance(None, list)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'list'>I make the statement True
48 def test_is_none_a_list(self): 49 self.assertIsNotNone([]) 50 self.assertIsNotNone([1, 2, 3, 'n']) 51 self.assertIsInstance([], list) 52 self.assertIsInstance([1, 2, 3, 'n'], list) 53 self.assertNotIsInstance(None, list) 54 55 56# NOTESthe test passes
I add a new note
57# NOTES 58# None is NOT a list 59# None is NOT a tuple 60# None is NOT a string 61# None is NOT a float 62# None is NOT an integer 63# None is NOT a boolean 64# None is None
test_is_none_a_set
RED: make it fail
I want to see if None is a set
53 self.assertNotIsInstance(None, list)
54
55 def test_is_none_a_set(self):
56 self.assertIsNone(set())
the terminal shows AssertionError
AssertionError: set() is not None
GREEN: make it pass
I make the statement True
56 self.assertIsNotNone(set())
the test passes
REFACTOR: make it better
I add another assertion
56 self.assertIsNotNone(set()) 57 self.assertIsNone({1, 2, 3, 'n'})the terminal shows AssertionError
AssertionError: {1, 2, 3, 'n'} is not NoneI make the statement True
57 self.assertIsNotNone({1, 2, 3, 'n'})the test passes
I add an instance test
57 self.assertIsNotNone({1, 2, 3, 'n'}) 58 self.assertNotIsInstance({1, 2, 3, 'n'}, set)the terminal shows AssertionError
AssertionError: {1, 2, 3, 'n'} is an instance of <class 'set'>I make the statement True
58 self.assertIsInstance({1, 2, 3, 'n'}, set)the test passes
I add another instance test
58 self.assertIsInstance({1, 2, 3, 'n'}, set) 59 self.assertIsInstance(None, set)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'set'>I make the statement True
55 def test_is_none_a_set(self): 56 self.assertIsNotNone(set()) 57 self.assertIsNotNone({1, 2, 3, 'n'}) 58 self.assertIsInstance({1, 2, 3, 'n'}, set) 59 self.assertNotIsInstance(None, set) 60 61 62# NOTESthe test passes
I add a note
62# NOTES 63# None is NOT a set 64# None is NOT a list 65# None is NOT a tuple 66# None is NOT a string 67# None is NOT a float 68# None is NOT an integer 69# None is NOT a boolean 70# None is None
test_is_none_a_dictionary
RED: make it fail
One last test to see if None is a dictionary
59 self.assertNotIsInstance(None, set)
60
61 def test_is_none_a_dictionary(self):
62 self.assertIsNone(dict())
63
64
65# NOTES
the terminal shows AssertionError
AssertionError: {} is not None
wait a minute! Python uses {} for sets. it also uses them for dictionaries with a difference.
GREEN: make it pass
I make the statement True
62 self.assertIsNotNone(dict())
the test passes
REFACTOR: make it better
I add another failing line
62 self.assertIsNotNone(dict()) 63 self.assertIsNone({'key': 'value'})the terminal shows AssertionError
AssertionError: {'key': 'value'} is not NoneI make the statement True
63 self.assertIsNotNone({'key': 'value'})the test passes
I add a failing instance test
63 self.assertIsNotNone({'key': 'value'}) 64 self.assertNotIsInstance({}, dict)dict is the class for dictionaries, the terminal shows AssertionError
AssertionError: {} is an instance of <class 'dict'>{}is the empty dictionaryI change the assert method
64 self.assertIsInstance({}, dict)the test passes
I add another instance test
64 self.assertIsInstance({}, dict) 65 self.assertNotIsInstance({'key': 'value'}, dict)the terminal shows AssertionError
AssertionError: {'key': 'value'} is an instance of <class 'dict'>{'key': 'value'}is a dictionary with:separating the key on the left from the value on the right, I can add more key-value pairs separating them with commas. sets do NOT have key-value pairs.I make the statement True
65 self.assertIsInstance({'key': 'value'}, dict)the test passes
I add the last failing instance test with assertIsInstance
65 self.assertIsInstance({'key': 'value'}, dict) 66 self.assertIsInstance(None, dict)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'dict'>I make the statement True with assertNotIsInstance
61 def test_is_none_a_dictionary(self): 62 self.assertIsNotNone(dict()) 63 self.assertIsNotNone({'key': 'value'}) 64 self.assertIsInstance({}, dict) 65 self.assertIsInstance({'key': 'value'}, dict) 66 self.assertNotIsInstance(None, dict) 67 68 69# NOTESthe test passes
I add the last note
69# NOTES 70# None is NOT a dictionary 71# None is NOt a set 72# None is NOT a list 73# None is NOT a tuple 74# None is NOT a string 75# None is NOT a float 76# None is NOT an integer 77# None is NOT a boolean 78# None is None
close the project
I close the file(s) I have open in the editor(s)
I click in the terminal and exit the tests with ctrl+c on the keyboard
I deactivate the virtual environment
deactivatethe terminal goes back to the command line,
(.venv)is no longer on the left side.../pumping_python/noneI change directory to the parent of
nonecd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory
review
I used assert methods to test what None is and what it is NOT. I used 2 that were say_hellod in AssertionError
assertIsNone - which tests if the thing in parentheses is None
assertIsNotNone - which tests if the thing in parentheses is not None
and 2 new assert methods
assertIsInstance which checks if something is an instance of a given class
assertNotIsInstance which checks if something is NOT an instance of a given class
I also showed the basic Python data structures
None - the simplest
integers - whole numbers, negative and positive
floats - floating point decimal numbers
tuples - anything in parentheses (
()) separated by commaslists - anything in square brackets (
[]) separated by commassets - anything in curly braces (
{}) separated by commas but NOT key-value pairsdictionaries - key-value pairs in curly braces (
{})
How many questions can you answer after going through this chapter?
code from the chapter
what is next?
so far you have covered