optimization question

Terry Reedy tjreedy at udel.edu
Mon Aug 12 21:41:33 CEST 2002

"Andrew Koenig" <ark at research.att.com> wrote in message
news:yu99r8h45bac.fsf at europa.research.att.com...
> >> I'd like to take a step back for a moment.  I originally asked a
> >> simple question:
> Michael> Which did get answered in amongst all the noise, yes?
> Yes.

Actually, I don't think so, not quite completely.

If seq is one of the standard, builtin, Python sequence types (string,
tuple, list), you can count on seq[i:j] being a new, separate object.
That is part of the language definition.  In the context of
comparisons, you can count on it *not* being optimized - it is not a

On the other hand...

If seq is a Numerical Python array, then slices are quite different:
they are 'views' of the original object.  No data is copied unless you
explicity so request.   The reason is the one that motivated you:
avoiding sloshing around megabytes of date more than necessary.  I
presume comparisons of arrays and maybe even of an array to a Python
sequence are done in the 'optimized' manner you were hoping for.
Since NP arrays can be arrays of chars or even generic arrays of
Python objects, this might be a solution for you.  For analysis of
large data sets, NP arrays should have other adantages as well
(including access to all the stuff already written for them).

Terry J. Reedy

More information about the Python-list mailing list