None
In Python None
is a special value. It is a value that indicates no value. It is often returned by collections (such as dictionaries) or methods.
We must handle None
in a special way. We cannot call methods, such as len()
, on a None
value. A TypeError
may result from invalid usage.
We show a common TypeError
that occurs with None
values. We call len()
on a string
. But when we change that variable to point to None
, len()
no longer works.
len()
built-in raises a TypeError
. The NoneType
has no len()
method.None
, we must first use an if
-statement to check for this condition.# Get length of this string. s = "value" print(len(s)) # Cannot get length of None. s = None print(len(s))5 Traceback (most recent call last): File "...", line 7, in <module> print(len(s)) TypeError: object of type 'NoneType' has no len()
TypeError
fixWe next provide a fix for incorrectly using a None
reference. In test()
, we check the parameter "v" against none in an if
-statement. If the value is not None
, we use len()
.
None
, we instead print a special value (-1). This avoids the TypeError
.def test(v): # Test for None. # ... Print -1 for length if None. if v != None: print(len(v)) else: print(-1) # Use None argument. test(None) # Use string argument. test("hello")-1 5
Dictionary
None
is used throughout Python types. It is often tested when accessing elements in a dictionary. Here we call the get method on the dictionary on a key that is not stored.
None
. We test for this in an if
-statement. We often cannot use the result of get()
directly.None
acts as a special value to the dictionary meaning "not found." None
can have special meanings based on the type.items = {"cat" : 1, "dog" : 2, "piranha" : 3} # Get an element that does not exist. v = items.get("giraffe") # Test for it. if v == None: print("Not found")Not found
None
is not the same thing as empty. A list (or string
) can be empty. An empty list has length of 0, and an empty string
equals the literal "".
None
, they instead point to no objects. This means the objects are "not present."len()
on a None
list or string
, you get a TypeError
. It does not return zero even though there are no elements.# This is an empty list of length 0. values = [] print(len(values)) # This is a nonexistent (None) list, with no length. values = None print(len(values))0 Traceback (most recent call last): File "...", line 8, in <module> print(len(values)) TypeError: 'NoneType' has no length
We can test for a None
value with not. This is a clearer syntax form than testing against the None
constant. Usually the clearer, shortest syntax is best.
value = "gerbil" if not value: print("A") # Not reached. value = None if not value: print("B") # "Not" matches None value.B
A method returns None
when no return value is specified. So we can store the result of any method. Here, we return 1 in a certain situation, but otherwise just return None
.
None
is a good way for a method to indicate "no answer" was found. This matches the design of dictionary get()
as well.def find(n): # This returns a value only if n equals 2. # ... Otherwise it returns None. if n == 2: return 1 # The method returns None. result = find(3) print(result)None
The None
value is used throughout Python programs. As a special value, it must be specially used. It is often returned by types like dictionaries.
TypeError
We encounter a TypeError
when we try to use None
in an invalid way. And we can fix this problem by checking for it, as with an if
-statement.