Home
Map
None: TypeError, NoneType Has No LengthUse the None value and protect against TypeErrors. Test for None in an if-statement.
Python
This page was last reviewed on Apr 14, 2023.
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.
Usage. 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.
An example. 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.
Info The len() built-in raises a TypeError. The NoneType has no len() method.
Tip If a variable may equal 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 fix. We 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().
However If the parameter "v" happens to equal 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.
Detail This method returns None. We test for this in an if-statement. We often cannot use the result of get() directly.
Tip Here None acts as a special value to the dictionary meaning "not found." None can have special meanings based on the type.
Dictionary
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
Empty. 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 "".
And When these values are None, they instead point to no objects. This means the objects are "not present."
So When you try to use 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
Not. 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.
if
value = "gerbil" if not value: print("A") # Not reached. value = None if not value: print("B") # "Not" matches None value.
B
Def. 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.
Tip 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
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 14, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.