List slice assignment and custom sequences

Terry Reedy tjreedy at udel.edu
Tue Nov 5 13:15:18 EST 2002


Ronald Oussoren <oussoren at cistron.nl> writes:

> No, I want to replace part of a sequence by another sequence. I
don't
> understand _why_ the RHS must be a list if the LHS is one.

The immediate, begging-the-question answer is that that is how slice
assignment is defined and hence, how it is programmed.  Lang Ref 6.3:
"If the target is a slicing: The primary expression in the reference
is evaluated. It should yield a mutable sequence object (e.g., a
list). The assigned object should be a sequence object of the same
type."

But why define it so?  Possibly simple pragmatics -- its easiest to
program that way (as per M. Hudson's answer).  The other is the
constant dilemma facing the language definer: if something could be
correct, but could be an error, should the interpreter raise a flag?
or do the 'obvious' thing? -- which in this case is to call list() or
act as if it had been (which would actually be more efficient).  Error
in this case seems unlikely enough that generalization seems
reasonable.

A third answer is inertia.  Python has been moving to a more
generalized notion of sequence and even iterable but slice assignment
got overlooked -- until you gave a push.  We'll see what happens for
2.3.

Terry J. Reedy





More information about the Python-list mailing list