language design question

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Jul 9 19:29:09 EDT 2006


On Sun, 09 Jul 2006 12:19:13 -0500, Gregory Guthrie wrote:

> I am comparing Python to a few other scripting languages, and used a simple 
> anagrams program as a sample.
> 
> I was surprised ast a few python features that did not work as I would 
> expect/wish; which caused less compact/expressive program styles that I 
> wanted - reverting to a FORTRAN like series of assignments.
> 
> For example,
>    - why is len() not a member function of strings? Instead one says len(w).

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?

>   - 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?

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.


>     This would allow things like:
>         key = '',join( list(word.lower().strip()).sort() )
>     instead:
>         key = ...
>         key.sort()
>         key = ...

One liners are over-rated.



>    - Another feature I assumed but it failed, is a nice default for 
> dictionaries, and more += like operations;

What default should this dictionary have?

{'key1' = 'alpha', 'key2' = 1, 'key3' = [4, 7, 3]}

Should it be '', 0, [], None or something else? Be prepared to justify
why your choice is suitable not just for *this* dictionary, but for *all*
dictionaries.


> I am curious why these "obvious" conveniences are not present.  :-)
> Thansk for any context or insight.


Perhaps the best way to understand the Python philosophy is to call up an
interpreter and execute "import this" at the prompt.


-- 
Steven.




More information about the Python-list mailing list