Top and Bottom Values [PEP: 326]

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Sep 27 09:37:40 EDT 2006


"Antoon Pardon" <apardon at forel.vub.ac.be> wrote in message 
news:slrnehkqqc.rcu.apardon at rcpc42.vub.ac.be...
> On 2006-09-27, Peter Otten <__peter__ at web.de> 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"

print Top
print int(Top)
print int(Top-1e9)
print Bottom
print Bottom+sys.maxint
print int(Bottom+sys.maxint)

a = range(10)
print a[:Top]
print a[Top:]
print a[3:Top]
print 3+Top
print Top+3
print 5+2/6*Top
print a[Bottom:3]

for i in xrange(Top):
    print i
    if i > 10: break


prints:

Top
2147483647
2147483647
Bottom
Bottom
-2147483648
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[]
[3, 4, 5, 6, 7, 8, 9]
Top
Top
Top
0
1
2
3
4
5
6
7
8
9
10
11
[0, 1, 2] 





More information about the Python-list mailing list