Small Bug?

Raymond Hettinger othello at javanet.com
Mon Dec 10 19:28:34 EST 2001


I was testing the type/class unification in 2.2b2 and noted a change in
behavior between a sub-class of list and a sub-class of UserList.

This is excepted from library code for UserList.Py in Python 2.1.1:

    def __getslice__(self, i, j):
        i = max(i, 0); j = max(j, 0)
        return self.__class__(self.data[i:j])

It shows that slices of a userlist are intensionally cast to retain the
class object being sliced.

In the Python2.2b2, this behavior changes and the slice is returned as a
list rather than the type of object being sliced.

This code shows the difference:

import UserList

class L(UserList.UserList):
    pass

p = L(['a','b','c','d'])
print p.__class__                # Original object is of class L
print p[1:].__class__          # Sliced object is also of class L

class M(list):
    pass

q = M(['a','b','c','d'])
print q.__class__                # Original object is of class M
print q[1:].__class__          # Sliced object in NOT of class M, but has
become a list!



Raymond Hettinger
othello at javanet dot com


P.S. I love the new Python.  It's great to not have to use 1L anymore to
avoid overflow. Generators are cool. Nested scopes are cool. The new divison
operator is a step in the right direction.  Thanks everyone for putting this
together in time for Christmas.










More information about the Python-list mailing list