LWN.net Logo

Python 2.5 released

Python 2.5 released

Posted Sep 21, 2006 20:01 UTC (Thu) by kbob (guest, #1770)
In reply to: Python 2.5 released by dofwno
Parent article: Python 2.5 released

It's already available in Python 2.2.

#!/usr/bin/python

class ArbitraryLowerBoundList(list):

    def __init__(self, lower_bound=0, *args, **kwargs):
        super(ArbitraryLowerBoundList, self).__init__(*args, **kwargs)
        self.__offset = lower_bound

    def lower_bound(self):
        return self.__offset

    def upper_bound(self):
        return self.__offset + len(self)

    def __setitem__(self, index, item):
        s = super(ArbitraryLowerBoundList, self)
        return s.__setitem__(index - self.__offset, item)

    def __getitem__(self, index):
        s = super(ArbitraryLowerBoundList, self)
        return s.__getitem__(index - self.__offset)

if __name__ == '__main__':
    a = ArbitraryLowerBoundList(10, ['a', 'b', 'c'])
    assert a.lower_bound() == 10
    assert a.upper_bound() == 13
    assert a[10] == 'a'
    a[11] = 'B'
    assert a[11] == 'B'
    assert a == ['a', 'B', 'c']


(Log in to post comments)

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds