Newbie Questions: Swithing from Perl to Python
Roy Smith
roy at panix.com
Sun Oct 26 09:03:19 EST 2003
In article <bnfmnv$fqd$1 at panix3.panix.com>, aahz at pythoncraft.com (Aahz)
wrote:
> In article <roy-D86C06.22163525102003 at reader1.panix.com>,
> Roy Smith <roy at panix.com> wrote:
> >
> >My personal opinion is that you should be able to do the simplier:
> >
> >for key in myDict.keys().sort()
> > print key
> >
> >but unfortunately, sort doesn't work like that. It sorts the list
> >in-place and does NOT return the sorted list.
>
> Yup. Guido doesn't want you copying the list each time you sort; it's
> easy enough to make your own copy function. Nevertheless, it appears
> likely that 2.4 will grow list.sorted() (yes, a static method on the
> list type).
What do you mean by "a static method on the list type"? Will I be able
to do:
for key in myDict.keys().sorted():
print key
and get what I expect? If so, then I think that's the behavior that
most people have found wanting in the current implementation and thus
will make a lot of people happy. It certainly will make me happy :-)
If that's what you're talking about, there's an obvious downside, which
is that now we'll have list.sort() and list.sorted() which do two
different things. This will be confusing.
Is there a PEP on this I could read? A quick look at the PEP index
didn't show anything that looked appropos.
I certainly understand the efficiency aspects of in-place sorting, but
this has always seemed like premature optimization to me. Most of the
time (at least in the code I write), the cost of an extra copy is
inconsequential. I'll be happy to burn a few thousand CPU cycles if it
lets me avoid an intermediate assignment or a couple of extra lines of
code. When things get too slow, then is the time to do some profiling
and figure out where I can speed things up.
More information about the Python-list
mailing list