copysort patch, was RE: [Python-Dev] inline sort option
Raymond Hettinger
python at rcn.com
Sat Oct 18 02:52:46 EDT 2003
[Raymond]
> > FWIW, I've posted a patch to implement list.copysort() that
> > includes a news announcement, docs, and unittests:
> >
> > www.python.org/sf/825814
[Guido]
> Despite my suggesting a better name, I'm not in favor of this (let's
> say -0).
>
> For one, this will surely make lots of people write
>
> for key in D.keys().copysort():
> ...
>
> which makes an unnecessary copy of the keys. I'd rather continue to
> write
>
> keys = D.keys()
> keys.sort()
> for key in keys:
> ...
Interesting that you saw this at the same time I was fretting about
it over dinner. The solution is to bypass the copy step for the
common case of: for elem in somelistmaker().copysort(): . . .
The revised patch is at:
www.python.org/sf/825814
The technique is to re-use the existing list whenever the refcount
is one. This keeps the mutation invisible.
Advantages of a copysort() method:
* Avoids creating an unnecessary, stateful variable that remains
visible after the sort is needed. In the above example, the
definition
of the "keys" variable changes from unsorted to sorted. Also, the
lifetime of the variable extends past the loop where it was intended
to be used.
In longer code fragments, this unnecessarily increases code
complexity,
code length, the number of variables, and increases the risk of using
a variable in the wrong state which is a common source of programming
errors.
* By avoiding control flow (the assignments in the current approach),
an inline sort becomes usable anywhere an expression is allowed. This
includes important places like function call arguments and list
comprehensions:
todo = [t for t in tasks.copysort() if due_today(t)]
genhistory(date, events.copysort(key=incidenttime))
Spreading these out over multiple lines is an unnecessary
distractor from the problem domain, resulting is code that is
harder to read, write, visually verify, grok, or debug.
Raymond Hettinger
P.S. There are probably better names than copysort, but the idea
still holds.
More information about the Python-Dev
mailing list