No, Python __hex__() Does Not Exist. Do This Instead!

Rate this post

The Problem

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 hex(x) function in Python when calling it on a My_Class object x.
  • You know about the feature of Dunder Methods to overwrite the custom behavior of built-in functions.
  • You conclude that the dunder method for hex() is __hex__() 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 __hex__(self): # WRONG!
        return 42


x = My_Class()
print(hex(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(hex(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 hex() function using the __hex__() method is to forget about the __hex__() method and use the __index__() method instead.

The __index__() method returns an integer value associated with a given object. The integer is then automatically converted to a hexadecimal 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(hex(x))
# 0x2a

In case you’re wondering about the output 0x2a instead of what you’ve defined, i.e., 42, the integer 42 is converted to the hexadecimal number 0x2a. See here:

>>> hex(42)
'0x2a'

Background hex() – Not a Magic Trick

Python’s built-in hex(integer) function takes one integer argument and returns a hexadecimal string with prefix "0x".

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.

Join the free webinar now!