LWN.net Logo

Python 2.5 released

Python 2.5 released

Posted Sep 19, 2006 21:51 UTC (Tue) by dofwno (guest, #40609)
Parent article: Python 2.5 released

When can we expect to get arrays with arbitrary lower indices?


(Log in to post comments)

Python 2.5 released

Posted Sep 20, 2006 12:06 UTC (Wed) by skaya (guest, #31233) [Link]

If by "arbitrary lower indices" you mean "negative indices", methinks this will not happen, since myarray[-1] means "the last element of myarray". But you can use a hashtable for that ;-)

If you mean "an array which starts at position 10000" (for instance), I think you can achieve that with a simple wrapper around getitem/setitem.

... If you mean something else, I would be grateful if you could clarify ?

Python 2.5 released

Posted Sep 20, 2006 14:25 UTC (Wed) by tjc (subscriber, #137) [Link]

Just curious: why do you want arrays with arbitrary lower indices?

Python 2.5 released

Posted Sep 21, 2006 20:01 UTC (Thu) by kbob (guest, #1770) [Link]

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']

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