[Python-bugs-list] [ python-Bugs-491398 ] Sliced list subclass has wrong type
noreply@sourceforge.net
noreply@sourceforge.net
Mon, 10 Dec 2001 19:37:02 -0800
Bugs item #491398, was opened at 2001-12-10 18:17
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=491398&group_id=5470
>Category: Type/class unification
Group: Python 2.2
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: Sliced list subclass has wrong type
Initial Comment:
In Python2.2b2, there is a change in behavior between
a sub-class of list and a sub-class of UserList.
The library code for UserList.Py in Python 2.1.1 shows
an intent to maintain the class as an invariant by
coercing the object being sliced.
def __getslice__(self, i, j):
i = max(i, 0); j = max(j, 0)
return self.__class__(self.data[i:j])
In the Python2.2b2, this behavior changes and the
slice is returned as a list rather than the class of
the object being sliced.
Example code showing 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 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 becomes a list!
----------------------------------------------------------------------
>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-12-10 19:37
Message:
Logged In: YES
user_id=6380
Subclassing list will never completely replace subclassing
UserList -- they serve different purposes. What UserList
does is a bit against the rules, IMO: it makes a requirement
on the constructor of the subclass which may be
inconvenient. But if you want that behavior, you can
override __getslice__ yourself.
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=491398&group_id=5470