strings and sort()

Paul Rubin phr-n2002a at nightsong.com
Thu Feb 21 03:12:34 EST 2002


"Sean 'Shaleh' Perry" <shalehperry at attbi.com> writes:
> > I don't understand this reply.  The sort method would be no less efficient
> > if it returned the sequence:
> > 
> >   def nsort(a):
> >      a.sort()
> >      return a
> > 
> of course it would.  returning has a cost.  Most uses of sort() just
> need to in place update the list.  If there was actually a copy made
> as some people expect that would be even more costly.

Huh?  The current version of sort returns None, which has exactly
the same cost as returning the sorted list.  If you look at the
definition of nsort above, it doesn't copy the list.  It sorts it
in place and then returns the sorted list instead of returning None.

> When programming in a language, you learn the idioms of that language.

When an "idiom" is really a "workaround", it's sometimes preferable
to fix it rather than to get used to it.



More information about the Python-list mailing list