Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

Python None Use: TypeError

This Python article uses the None value and protects against TypeErrors. It tests 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. This fix is shown later in this page.

Based on:

Python 3

Python program that assigns to None

# Get length of this string.
s = "value"

# Cannot get length of None.
s = None


Traceback (most recent call last):
  File "...", line 7, in <module>
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:

# Use None argument.

# Use string argument.



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")


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 = []

# This is a nonexistent (None) list, with no length.
values = None


Traceback (most recent call last):
  File "...", line 8, in <module>
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.



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)



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.