Subclassing int in Python

20,753

Solution 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()

Solution 2

Though correct the current answers are potentially not complete.

e.g.

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'>

UPDATE:
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.

Share:
20,753
me_and
Author by

me_and

L3 support engineer for Metaswitch Networks (stuff posted here isn't on behalf of my company &amp;c. &amp;c.). I hack around in Python, spend more time than I'd like with shell, and am proficient if somewhat rusty with C. I'm also the Git maintainer for Cygwin and a contributor to Dreamwidth. You can also find me in a wide variety of larp fields, or on Twitter, Facebook, Google+, Wikipedia (which has by far the most complete profile), and other places by request.

Updated on January 11, 2020

Comments

  • me_and
    me_and over 4 years

    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
    0
    

    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