TypeError: '...' object cannot be interpreted as an integer
If you’re reading this article, chances are that you have been thinking something along those lines:
- Given a custom class
My_Class. You want to override the behavior of the built-in
oct(x)function in Python when calling it on a
- You know about the feature of Dunder Methods to overwrite the custom behavior of built-in functions.
- You conclude that the dunder method for
__oct__()because, after all, this is how it is supposed to work, right?
- Wrong! If you try to do it, you get the following error.
The Wrong Example – Don’t!
class My_Class: def __oct__(self): # WRONG! return 42 x = My_Class() print(oct(x))
The output indicates that something went wrong:
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 7, in <module> print(oct(x)) TypeError: 'My_Class' object cannot be interpreted as an integer
What To Do?
How can you resolve the
TypeError: 'My_Class' object cannot be interpreted as an integer?
To resolve the
TypeError when trying to override the behavior of the
oct() function using the
__hex__() method is to forget about the
__oct__() method and use the
__index__() method instead.
__index__() method returns an integer value associated with a given object. The integer is then automatically converted to an octal number.
The Right Example – Do!
Here’s how this works in a simple example:
class My_Class: def __index__(self): # Correct! return 42 x = My_Class() print(oct(x)) # 0o52
In case you’re wondering about the output
0o52 instead of what you’ve defined, i.e.,
42, the integer 42 is converted to the octal number
0o52. See here:
>>> oct(42) '0o52'
Background oct() – Not a Magic Trick
oct(integer) function takes one integer argument and returns an octal string with prefix
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.