Subclassing int in Python

Posted on

Question :

Subclassing int in Python

I’m interested in subclassing the built-in int type in Python (I’m using v. 2.5), but having some trouble getting the initialization working.

Here’s some example code, which should be fairly obvious.

class TestClass(int):
    def __init__(self):
        int.__init__(self, 5)

However, when I try to use this I get:

>>> a = TestClass()
>>> a

where I’d expect the result to be 5.

What am I doing wrong? Google, so far, hasn’t been very helpful, but I’m not really sure what I should be searching for

Asked By: me_and


Answer #1:

int is immutable so you can’t modify it after it is created, use __new__ instead

class TestClass(int):
    def __new__(cls, *args, **kwargs):
        return  super(TestClass, cls).__new__(cls, 5)

print TestClass()
Answered By: me_and

Answer #2:

Though correct the current answers are potentially not complete.


a = TestClass()
b = a - 5
print type(b)

Would show b as an integer, where you might want it to be a TestClass.

Here is an improved answer

class positive(int):
    def __new__(cls, value, *args, **kwargs):
        if value < 0:
            raise ValueError("positive types must not be less than zero")
        return  super(cls, cls).__new__(cls, value)

    def __add__(self, other):
        res = super(positive, self).__add__(other)
        return self.__class__(max(res, 0))

    def __sub__(self, other):
        res = super(positive, self).__sub__(other)
        return self.__class__(max(res, 0))

    def __mul__(self, other):
        res = super(positive, self).__mul__(other)
        return self.__class__(max(res, 0))

    def __div__(self, other):
        res = super(positive, self).__div__(other)
        return self.__class__(max(res, 0))

    def __str__(self):
        return "%d" % int(self)

    def __repr__(self):
        return "positive(%d)" % int(self)

Now the same sort of test

>>> a = positive(10)
>>> b = a - 9
>>> print(type(b))
<class '__main__.positive'>

Added repr and str examples so that the new class prints itself properly. Also changed to Python 3 syntax, even though OP used Python 2, to maintain relevancy.

Answered By: Anurag Uniyal

Leave a Reply

Your email address will not be published.