language design question

Paul Rubin http
Sun Jul 9 20:56:53 EDT 2006


Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
> Length is an obvious property of any one-dimensional non-scalar, not just
> strings. As such, it makes sense to have a length function that takes an
> argument. As a design decision, it could go either way, but early
> Python wasn't fully object-oriented. If Guido was designing Python
> from scratch today, who knows whether he'd still make len a function?

There could be a multidimensional variant which gives the shape of an
array, like the APL rho operator.  RMS once wrote a song about it
(tune of "Row, row, row your boat"):

  Rho rho rho of x, always equals 1, 
  Rho's dimension, rho rho rank, APL is fun!

> >   - Why doesn't sort() return a value?
> 
> Because if you have a 100 MB list of objects which you want to sort, why
> go to the significant expense of making a sorted copy only to throw the
> original away?

.sort() could sort in place and return the sorted list instead of returning None.

> In general, Python never copies data unless you explicitly tell it too. If
> you want to keep the original, unsorted list around, you need to make a
> copy of it explicitly.

The convention actually is that mutating operations like .sort()
return None, which supposedly helps avoid certain types of errors.
But it leads to tedious code, thus the addition of sorted().  sorted()
of course has the nice property of being able to operate on arbitrary
sequences, which was one of the (valid) justifications for making it a
function.

The Lisp convention would be to name the function "nsort" (like
nreverse, nconc, etc.)  where the "n" signifies that the operation is
in-place.  The Scheme convention is to suffix with "!", like "set!",
"sort!", "reverse!", and so forth.

> >     This would allow things like:
> >         key = '',join( list(word.lower().strip()).sort() )
> >     instead:
> >         key = ...
> >         key.sort()
> >         key = ...
> 
> One liners are over-rated.
> ...
> Perhaps the best way to understand the Python philosophy is to call up an
> interpreter and execute "import this" at the prompt.

"Beautiful is better than ugly".



More information about the Python-list mailing list