dictionary issue (and maybe PEP ... depending on the answer)

Terry Reedy tjreedy at udel.edu
Mon Jun 2 17:54:20 CEST 2003


> you want a non-destructive sort.  i want a non-destructive sort.
last
> week someone was wanting to add a non-destructive sort with mapping.
> every new user screws up assuming that [].sort is non-destructive.

Since all list modification methods work in place, I never made such a
special case assumption.  I don't quite understand why others would
either.  A 'non-destructive' .reverse() would make exactly as much
sense.

It is quite normal for mutable-object methods to mutate the object.
The separate issue is whether the methods should return the object so
multiple mutations can be chained together, or whether they should
return (a) nothing (indicator), as Python does, so people can't forget
for too long that the mutation methods do mutate.  (But then people do
forget that one cannot chain.)

> you'd think that someone would add non-destructive sort to python,

Some people who want it have done so with their own site setup.

def seqsort(seq):
  tem = list(seq)
  tem.sort()
  return tem

sef seqrev(seq):
  tem = list(seq)
  tem.reverse()
  return tem

or something like that.  To be fancier, store input type and convert
tem back to that type if possible.

Terry J. Reedy







More information about the Python-list mailing list