Encountering errors is a natural part of working in computer science. Whether you are a seasoned coding expert or a newbie, seeing some red text in a console accompanied by an error message is an experience that you will have often.
Python errors appear in red but that doesn’t mean you should be intimidated. Often, the solution to a problem is easy to find, you just need to know how to respond to the different types of errors that you may encounter.
In this guide, we break down the top eight Python TypeErrors and discuss how to solve them. By the end of reading this article, you’ll be equipped to handle a range of different errors you may encounter.
What is a TypeError?
A TypeError is raised when you try to perform an action on a value or a function whose data type does not support the action.
For instance, if you try to call a string object as if it were a function, you’ll receive an error; if you try to iterate over a None value, Python will raise an error. TypeErrors are one of the most common types of errors in Python.
‘module’ object is not callable
This error is raised when you try to call a module object in your code. To solve this error, reference the specific functions and variables you want to access from a module instead of calling an entire module.
a bytes-like object is required, not ‘str’
This error occurs if you treat a string as a bytes object. This error is raised if you open a file as a binary instead of a text file. Fix this error by opening all text files in a program in text mode (without a “b” in the open() statement).
string indices must be integers
Strings are indexed from zero. Every subsequent character in a string has an index value one greater than the last. This error occurs when you specify a string value as an index number instead of a valid integer.
To solve this error, only use integers to access items from an indexed object like a string or a list.
list indices must be integers or slices, not str
Like strings, lists are indexed from zero. You can only retrieve a value from a list by specifying an integer index value or a range of values using slicing notation.
To fix this error, use numbers to access items from a list. If you are trying to access items from a list of dictionaries, make sure you first access a dictionary before trying to access an item from the dictionary.
‘list’ object is not callable
To access items in a list, you should use index notation:
Index notation is where you enclose an index value in square brackets at the end of a value. The above code returns the first item in the “languages” list. This value is “Python”.
If you try to access a list using curly brackets instead of square brackets, you’ll see the “‘list’ object is not callable” error.
‘str’ object is not callable
The cause of this error is the same as the last error. You can only access items in a string using indexing notation. This means you need to use square brackets and specify the value in the string you want to retrieve. Using curly brackets returns an error.
‘nonetype’ object is not iterable
You can only use a “for” loop on an iterable object. This is because for loops can only iterate over a sequence of values. To solve this error, make sure the objects you are trying to iterate over are not equal to None before you iterate over them.
‘builtin_function_or_method’ object is not subscriptable
To call a built-in function or method, you must specify the name of that method, followed by a set of curly brackets. The curly brackets denote a function call.
This error is caused by using square brackets to call a function. To fix this error, use curly brackets to call a function.
There are only a finite number of TypeErrors that you can encounter. As you start to encounter more of these errors, you’ll figure out how they work and how you can solve them. Like anything in programming, the more practice you get, the more you will improve.
If you’re looking to level-up your knowledge of errors even further, check out a few of our more advanced tutorials on Python TypeErrors:
- object of type ‘NoneType’ has no len()
- can only concatenate list (not “int”) to list
- ‘float’ object is not subscriptable
- can’t multiply sequence by non-int of type ‘str’
- ‘function’ object is not subscriptable
When you encounter an error, the best thing you can do is read the message in detail. Most error messages tell you everything you need to know to fix an error once you understand how they work and what they mean.
About the author