language design question

Steven Bethard steven.bethard at gmail.com
Sun Jul 9 13:37:32 EDT 2006


Gregory Guthrie wrote:
> For example,
>    - why is len() not a member function of strings? Instead one says len(w).

Why would ``x.len()`` be any more convenient than ``len(x)``? Your 
preference here seems pretty arbitrary.

>   - Why doesn't sort() return a value?
> 
>     This would allow things like:
>         key = '',join( list(word.lower().strip()).sort() )

Use sorted():

     key = ','.join(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]

Get Python 2.5 and use collections.defaultdict:

Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import collections
 >>> d = collections.defaultdict(int)
 >>> d['a'] += 5
 >>> d['b'] += 2
 >>> d
defaultdict(<type 'int'>, {'a': 5, 'b': 2})

STeVe



More information about the Python-list mailing list