I'm creating a hex class for hexadecimal values. The __init__
method is expecting a hex string, but I want to be able to create a Hex instance from an integer in decimal, so I create a method for that. The code is as follows:
class Hex:
def __init__(self, val):
#val should be a hex string
self.val = val
def from_dec(self, x):
self.__init__(self, hex(x))
However, when I run:
a = Hex.from_dec(20)
print a.val
I get the following error:
TypeError: unbound method from_dec() must be called with Hex instance
as first argument (got int instance instead)
How do I use __init__
properly?