language design question

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sun Jul 9 13:50:48 EDT 2006


Gregory Guthrie a écrit :
> 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).

The member function of strings is __len__, which itself is called by 
len() when passed a string. wrt/ why it is so, you'll have to ask to 
someone more knowledgeable, but I seriously  don't see what difference 
it make in practice.

>   - Why doesn't sort() return a value?

If it's not in the FAQ, then it should - and the  googling c.l.py 
archives for this should give some relevant answers.

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

key = ''.join(list(sorted(word.lower().strip()))

>    - Another feature I assumed but it failed, is a nice default for 
> dictionaries, and more += like operations;
>    For example: to acculumate words in a dictionary -
>         dict[key] += [word]
> 
>      Instead of:
>         mark[key] = mark.get(key,[]) + [word]

mark.setdefault(key, []).append(word)

> The former seems very intuitive, and clearer.

and is much more error prone for few practical gain (assertion backed by 
experience with languages allowing it).

> I am a bit used to the compactness and convenient defaults of Perl, which 
> would do this:
>    my $key = join '', sort(split(//, lc($word)));
>     push @{$anagrams{$key}}, $word

key = ''.join(list(sorted(word.lower().strip()))
anagrams.setdefault(key, []).append(word)

> I am curious why these "obvious" conveniences are not present.  :-)

You said ?-)



More information about the Python-list mailing list