DanielStutzbach / blist

A list-like type with better asymptotic performance and similar performance on small lists
Other
313 stars 35 forks source link

keysview, valuesview accept some negative indices #74

Closed tallforasmurf closed 10 years ago

tallforasmurf commented 10 years ago

Writing a unit-test case for code that depends on a sorteddict, I test the case of passing invalid index values to a method that depends on a keysvalue[index] and a valuesview[index].

    def getstuff(self, j):
        try:
            return self.values_view[j]
        except:
            # report error

To my surprise I found that passing j<0 did not raise an exception, provided abs(j)<=len(self.values_view), in fact it returns self.values_view[len(self.values_view)+j] and only raises IndexError when the index is outside this mirror-range.

Is this expected behavior?

DanielStutzbach commented 10 years ago

Yes. This is how all sequences in Python work, such as list() and tuple().