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>Since python integers are unbounded, you have to do this with a custom class:</p>

<pre><code>import functools

@functools.total_ordering
class NeverSmaller(object):
    def __le__(self, other):
        return False

class ReallyMaxInt(NeverSmaller, int):
    def __repr__(self):
        return 'ReallyMaxInt()'
</code></pre>

<p>Here I've used a mix-in class <code>NeverSmaller</code> rather than direct decoration of <code>ReallyMaxInt</code>, because on Python 3 the action of <code>functools.total_ordering</code> would have been prevented by existing ordering methods inherited from <code>int</code>.  </p>

<p>Usage demo:</p>

<pre><code>&gt;&gt;&gt; N = ReallyMaxInt()
&gt;&gt;&gt; N &gt; sys.maxsize
True
&gt;&gt;&gt; isinstance(N, int)
True
&gt;&gt;&gt; sorted([1, N, 0, 9999, sys.maxsize])
[0, 1, 9999, 9223372036854775807, ReallyMaxInt()]
</code></pre>

<p>Note that in python2, <code>sys.maxint + 1</code> is bigger than <code>sys.maxint</code>, so you can't rely on that.  </p>

<p><em>Disclaimer</em>: This is an integer in the <a href="https://en.wikipedia.org/wiki/Object-oriented_programming#Inheritance_and_behavioral_subtyping" rel="nofollow">OO</a> sense, it is not an integer in the mathematical sense.  Consequently, arithmetic operations inherited from the parent class <code>int</code> may not behave sensibly.  If this causes any issues for your intended use case, then they can be disabled by implementing <code>__add__</code> and friends to just error out.</p>

python python-3.x