List slice assignment and custom sequences

Michael Hudson mwh at python.net
Tue Nov 5 13:26:53 EST 2002


"Terry Reedy" <tjreedy at udel.edu> writes:

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

I'm 99% sure this is what happened -- I think list_ass_subscript is
very old code, possibly even predating __getitem__ on instances (if
there was such a time).

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

One of the things used to be that handling user defined sequences is
subtle -- consider:

class Evil:
    def __len__(self):
        return 9
    def __getitem__(self, i):
        raise IndexError

Blindly trusting the answer of PyObject_Length() can easily lead to
core dumps.  But now Python has this wonderful PySequence_Fast API
that wraps up all the nasty details while keeping the common case
fast, so supporting user-defined sequences is now trivial.

Everyone writing C extensions should know about PySequence_Fast().

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

Indeed.

> We'll see what happens for 2.3.

He pushed; it fell :)

Cheers,
M.

-- 
  "Also, does the simple algorithm you used in Cyclops have a name?"
  "Not officially, but it answers to "hey, dumb-ass!"
                       -- Neil Schemenauer and Tim Peters, 23 Feb 2001



More information about the Python-list mailing list