Small Bug?

Tim Peters tim.one at home.com
Mon Dec 10 20:04:52 EST 2001


[Raymond Hettinger]
> 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.

It's different behavior, but not a change in behavior, since it wasn't
possible to subtype from list before 2.2.

> 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.

Yes, that's how it works.  There's a bug report on SourceForge that goes
into more detail, but the short course is that UserList behavior is
unsupportable in general, as the list implementation has no idea how a
subclass constructor needs to be called.  The UserList gimmick works in the
cases it works; so does subclassing list <wink>.

Guido recommends that you *not* try to override builtin behaviors when
subtyping from builtin types, but rather restrict yourself to adding new
methods and/or new data members.  If you think you have to override builtin
methods, then you're going to have to override every builtin method of
interest to you (even when you think the desired behavior "is obvious").

> ...
> 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.

Seems a pretty safe bet right now, but don't count your pythons before
they're hatched <wink>.





More information about the Python-list mailing list