[Python-checkins] r75051 - in python/branches/py3k: Lib/test/test_range.py Objects/rangeobject.c

Mark Dickinson dickinsm at gmail.com
Fri Sep 25 08:20:42 CEST 2009


On Thu, Sep 24, 2009 at 10:23 PM, Jim Jewett <jimjjewett at gmail.com> wrote:
> Why not subclasses?

This commit was a fix for a problem introduced in
r75028.  The change in r75028 was supposed to be
purely an optimization, with no behaviour changes.  After
committing it I realized that it did in fact change
behaviour for subclasses of int.

Specifically, before r75028, 'x in range(...)'
and 'x in list(range(...))' are interchangeable, apart
from memory and speed considerations.  After that
change this was no longer true for subclasses of int.

I agree that a well-designed int subclass is unlikely
to have this problem, but given how infrequently
int is subclassed anyway (bool excepted), it didn't
seem worth extending the optimization to subclasses
if that extension involved a potential behaviour change
for less well-designed int subclasses.

I'd actually be happy for 'x in range' to use the
__index__ value of x, as Nick originally suggested;
if that's what other people feel is appropriate.
However, that *would* be a behaviour change, and
would break the current invariant that 'x in range(...)'
is equivalent to 'x in list(range(...))';  such a change
probably needs a python-dev discussion, or at least
more discussion in the issue tracker, first.

Mark


More information about the Python-checkins mailing list