9. NoneType type#

Author: Tue Nguyen

9.1. Outline#

  • Why NoneType type?

  • How to get a NoneType?

  • When uses NoneType?

  • Operations on NoneType

9.2. Why NoneType type?#

  • Motivation: used to represent the state of being unknown/undefined in both data kind and data value

    • Note that being undefined is NOT the same as being empty

    • An empty list still belong to the list type

  • Real-life data: unknown values, missing values

  • Python implementation:

    • Type: NoneType

    • Possible values: None

9.3. How to get a NoneType?#

a) Ex 1: from the None literal

# Assign None to x
x = None
# Check type
type(x)
NoneType

Notes

  • When you evaluate None on a code cell of Jupyter Lab, you will not see anything printed out

  • To display None you have to explicitly print it out using print()

# None value will not show up this way
x
# Explicity print out the None value
print(x)
None

b) Ex 2: from an expression that produces None

# A print statement returns None
# Thus, after the assignment, x will be None, not Hello
x = print("Hello")
Hello

Notes

  • Many people misunderstand that x will assume the value "Hello" after the above assignment

  • It’s NOT true

  • "Hello" is what print prints out, it’s not what print returns

  • You will learn more about function later. For now, just accept that

    • print prints out "Hello"

    • But after the printing is done, it returns something else to the place where it is called

    • This returned value is None

# Let's confirm that x is indeed None
print(x)
print(type(x))
None
<class 'NoneType'>

9.4. When uses NoneType?#

  • The advantages of using NoneType might not be obvious at this stage

  • But it will become clear as learn more about Python

  • NoneType is normally used as a return option of a function to indicate that the function already did what you asked, but it couldn’t find anything that makes sense

  • Let’s consider an example

# Init a string
s = "This is a dog"
# Now, suppose we want to check if some other string is contained in `s`
# We can use re.search() function of module re
# Thus, we need to import module re` to use its search function
import re
# You can read the document for re.search for more details
?re.search
# We try to search for 'cat' is in s
found = re.search("cat", s)

# Since 'cat' is not in s, re.search returns None 
# to indicate that it couldn't find anything
print(found)
print(type(found))
None
<class 'NoneType'>
# Now, try to search for 'dog' in s
found = re.search("dog", s)

# Since 'dog' is indeed in s
# re.search returns something different from None
# which is a re.Match object that contains the position of the match
print(found)
print(type(found))
<re.Match object; span=(10, 13), match='dog'>
<class 're.Match'>

9.5. Operations on NoneType#

  • There is not much we can do with NoneType because it is so simple

  • The most commonly used operation is to check if a variable is actually None

  • To do this, we perform a comparison using is

a) Ex 1: simple examples

# Init two variables
x = None
y = 10
# Check if x is None
x is None
True
# Check if y is None
y is None
False
# Check if x is NOT None
x is not None
False
# Check if y is NOT None
y is not None
True

b) Ex 2: more interesting examples

# Init a string
s = "This is a dog"

b1) Find ‘dog’ in s

# Search for 'dog'
substr = "dog"
found = re.search(substr, s)
# If found, print out the starting and ending indices
# Otherwise, print not found
if found is None:
    print(f"'{substr}' is not in s")
else:
    start = found.start()
    end = found.end()
    print(f"Found {substr} in s beginning at {start} and ending at {end}")
Found dog in s beginning at 10 and ending at 13

b2) Find ‘cat’ in s

# Same as above, but now we search for 'cat'
substr = "cat"
found = re.search(substr, s)
# If found, print out the starting and ending indices
# Otherwise, print not found
if found is None:
    print(f"'{substr}' is not in s")
else:
    start = found.start()
    end = found.end()
    print(f"Found {substr} in s beginning at {start} and ending at {end}")
'cat' is not in s

Remarks:

  • We can also use == instead of is to check if a variable is None

  • But it is NOT recommended

  • For a detailed explanation, read here.

9.6. Summary#

Why NoneType?

  • Used to represent unknown or missing data

  • Use as a returned value of a function to indicate something. Ex: not found, or the function just does something and doesn’t really return anything so it returns None by default

How get a NoneType

  • From the None literal

    • Ex: x = None

  • From an expression that produces None

    • Ex: x = re.search("cat", "This is a dog")

When uses NoneType?

  • Used to represent unknown or missing data

  • Use as a returned value of a function to indicate something

Operations on NoneType?

  • Check if a variable is or is NOT None

    • Ex 1: x is None

    • Ex 2: x is not None

  • We can use == instead of is, but this practice is NOT recommended

9.7. Practice#

To be updated