How to make an integer larger than any other integer?
<p>Note: while the accepted answer achieves the result I wanted, and @ecatmur answer provides a more comprehensive option, I feel it's very important to emphasize that my use case is a bad idea in the first place. This is explained very well in <a href="http://stackoverflow.com/a/39856605/336527">@Jason Orendorff answer below</a>.</p>

<p>Note: this question is not a duplicate of <a href="http://stackoverflow.com/questions/13795758/what-is-sys-maxint-in-python-3">the question about <code>sys.maxint</code></a>. It has nothing to do with <code>sys.maxint</code>; even in python 2 where <code>sys.maxint</code> is available, it does NOT represent largest integer (see the accepted answer).</p>

<p>I need to create an integer that's larger than any other integer, meaning an <code>int</code> object which returns <code>True</code> when compared to any other <code>int</code> object using <code>&gt;</code>. Use case: library function expects an integer, and the only easy way to force a certain behavior is to pass a very large integer.</p>

<p>In python 2, I can use <code>sys.maxint</code> (edit: I was wrong). In python 3, <code>math.inf</code> is the closest equivalent, but I can't convert it to <code>int</code>.</p>

<p>You should not be inheriting from <code>int</code> unless you want both its <em>interface</em> and its <em>implementation</em>.  (Its implementation is an automatically-widening set of bits representing a finite number.  You clearly dont' want that.)  Since you only want the <em>interface</em>, then inherit from the ABC <code>Integral</code>.  Thanks to @ecatmur's answer, we can use <code>infinity</code> to deal with the nitty-gritty of infinity (including negation).  Here is how we could combine <code>infinity</code> with the ABC <code>Integral</code>:</p>

<pre><code>import pytest
from infinity import Infinity
from numbers import Integral


class IntegerInfinity(Infinity, Integral):

    def __and__(self, other):
        raise NotImplementedError

    def __ceil__(self):
        raise NotImplementedError

    def __floor__(self):
        raise NotImplementedError

    def __int__(self):
        raise NotImplementedError

    def __invert__(self, other):
        raise NotImplementedError

    def __lshift__(self, other):
        raise NotImplementedError

    def __mod__(self, other):
        raise NotImplementedError

    def __or__(self, other):
        raise NotImplementedError

    def __rand__(self, other):
        raise NotImplementedError

    def __rlshift__(self, other):
        raise NotImplementedError

    def __rmod__(self, other):
        raise NotImplementedError

    def __ror__(self, other):
        raise NotImplementedError

    def __round__(self):
        raise NotImplementedError

    def __rrshift__(self, other):
        raise NotImplementedError

    def __rshift__(self, other):
        raise NotImplementedError

    def __rxor__(self, other):
        raise NotImplementedError

    def __trunc__(self):
        raise NotImplementedError

    def __xor__(self, other):
        raise NotImplementedError

def test():
    x = IntegerInfinity()
    assert x &gt; 2
    assert not x &lt; 3
    assert x &gt;= 5
    assert not x &lt;= -10
    assert x == x
    assert not x &gt; x
    assert not x &lt; x
    assert x &gt;= x
    assert x &lt;= x
    assert -x == -x
    assert -x &lt;= -x
    assert -x &lt;= x
    assert -x &lt; x
    assert -x &lt; -1000
    assert not -x &lt; -x
    with pytest.raises(Exception):
        int(x)
    with pytest.raises(Exception):
        x | x
    with pytest.raises(Exception):
        ceil(x)
</code></pre>

<p>This can be run with <code>pytest</code> to verify the required invariants.</p>

python python-3.x