Python None: TypeError, NoneType Has No Length

Use the None value and protect against TypeErrors. Test for None in an if-statement.

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.
Instead: 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.
Python program that assigns to None # Get length of this string. s = "value" print(len(s)) # Cannot get length of None. s = None print(len(s)) Output 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.
Python program that tests None 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") Output -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.
Get: 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.
Python program that uses dictionary, None 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") Output 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.
Python program that uses empty list, None # 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)) Output 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, Not
Python program that uses not, None value = "gerbil" if not value: print("A") # Not reached. value = None if not value: print("B") # "Not" matches None value. Output 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.
Python program that uses def, returns None 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) Output 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.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.