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>Another way to do this (very much inspired by wim's answer) might be an object that isn't infinite, but increases on the fly as needed. </p>

<p>Here's what I have in mind: </p>

<pre><code>from functools import wraps

class AlwaysBiggerDesc():
    '''A data descriptor that always returns a value bigger than instance._compare'''
    def __get__(self, instance, owner):
        try:
            return instance._compare + 1
        except AttributeError:
            return instance._val
    def __set__(self, instance, value):
        try:
            del instance._compare
        except AttributeError:
            pass
        instance._val = value

class BiggerThanYou(int):
    '''A class that behaves like an integer but that increases as needed so as to be 
    bigger than "other" values. Defaults to 1 so that instances are considered
    to be "truthy" for boolean comparisons.'''
    val = AlwaysBiggerDesc()
    def __getattribute__(self, name):
        f = super().__getattribute__(name)
        try:
            intf = getattr(int,name)
        except AttributeError:
            intf = None
        if f is intf:
            @wraps(f)
            def wrapper(*args):
                try:
                    self._compare = args[1]
                except IndexError:
                    self._compare = 0 # Note: 1 will be returned by val descriptor
                new_bigger = BiggerThanYou()
                try:
                    new_bigger.val = f(self.val, *args[1:])
                except IndexError:
                    new_bigger.val =  f(self.val)
                return new_bigger
            return wrapper
        else:
            return f            
    def __repr__(self):
        return 'BiggerThanYou()'
    def __str__(self):
        return '1000...'
</code></pre>

<p>Something like this might avoid a lot of weird behavior that one might not expect. Note that with this kind of approach, if two <code>BiggerThanYou</code> instances are involved in an operation, the LHS would be considered bigger than the RHS. </p>

<p>EDIT: currently this is not working- I'll fix it later. it seems I am being bitten by the <a href="http://stackoverflow.com/a/13063764/2437514">special method lookup functionality</a>. </p>

python python-3.x