copysort patch, was RE: [Python-Dev] inline sort option

Alex Martelli aleaxit at yahoo.com
Sat Oct 18 11:43:38 EDT 2003


On Saturday 18 October 2003 05:16 pm, Skip Montanaro wrote:
> Guido> For one, this will surely make lots of people write
>> Guido> for key in D.keys().copysort():
> Guido> ...
>> Guido> which makes an unnecessary copy of the keys.
>> It might be viewed as unnecessary if you intend to change D's keys within
> the loop.

D.keys() makes a _snapshot_ of the keys of D -- it doesn't matter what
you do to D in the loop's body.
Admittedly, that's anything but immediately obvious (quite apart from
copysorting or whatever) -- I've seen people change perfectly good
code of the form:
 for k in D.keys():
 vastly_alter_a_dictionary(D, k)
into broken code of the form:
 for k in D:
 vastly_alter_a_dictionary(D, k)
because of having missed this crucial difference -- snapshot in the
first case, but NOT in the second one. And viceversa, I've seen people
carefully copy.copy(D.keys()) or the equivalent to make sure they did
not suffer from modifying D in the loop's body -- the latter is in a sense
even worse, because the bad effects of the former tend to show up
pretty fast as weird bugs and broken unit-tests, while the latter is "just"
temporarily wasting some memory and copying time.
Anyway, copysort with The Trick, either as a method or function, has
no performance problems - exactly the same performance as:
> Guido> keys = D.keys()
> Guido> keys.sort()
> Guido> for key in keys:
> Guido> ...
>> Current standard practice is also fine.

Nolo contendere. It DOES feel a bit like boilerplate, that's all.
Alex


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /