On 2006-09-27, Paul McGuire <[EMAIL PROTECTED]> wrote:
> "Antoon Pardon" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
>> On 2006-09-27, Peter Otten <[EMAIL PROTECTED]> wrote:
>>> Antoon Pardon wrote:
>>>
>>>> I had written my own module, which works similarly but
>>>> is somewhat extended. Here is an example of how it can
>>>> be used and how I would like to use it but get stuck.
>>>>
>>>> from extreme import Top
>>>>>>> Top
>>>> Top
>>>>>>> Top + 1
>>>> Top
>>>>>>> Top - 30
>>>> Top
>>>>>>> Top > 1e99
>>>> True
>>>>>>> lst = range(10)
>>>>>>> lst[:Top]
>>>> Traceback (most recent call last):
>>>>   File "<stdin>", line 1, in ?
>>>> TypeError: slice indices must be integers or None
>>>>
>
> Here's a little more refined version of my previous post:
>
> class StubbornInt(int):
>     def invariantOp(self,other):
>         return self
>     __add__ = invariantOp
>     __sub__ = invariantOp
>     __mul__ = invariantOp
>     __div__ = invariantOp
>     __iadd__ = invariantOp
>     __isub__ = invariantOp
>     __imul__ = invariantOp
>     __idiv__ = invariantOp
>     __radd__ = invariantOp
>     __rsub__ = invariantOp
>     __rmul__ = invariantOp
>     __rdiv__ = invariantOp
>     def __str__(self):
>         return self.name
>
> import sys
> Top = StubbornInt(sys.maxint)
> Top.name = "Top"
> Bottom = StubbornInt(-sys.maxint-1)
> Bottom.name = "Bottom"

Well one problem I have with your solution is the following:

  >>> for x in xrange(sys.maxint - 2, Top):
  ...   print x
  ... 
  2147483645
  2147483646
  >>> 

The idea with a Top (and Bottom) variable is that you once and for
all have a value that is bigger (smaller) than any other value you are
going to use. AFAICS using a subclass of int will always
leave an opportunity open where an other value will be treated
as bigger (smaller), because functions like xrange will just
look at the int value of the object.

-- 
Antoon Pardon
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to