language design question

Terry Reedy tjreedy at udel.edu
Sun Jul 9 18:15:35 EDT 2006


"Gregory Guthrie" <guthrie at mum.edu> wrote in message 
news:1152465029_31209 at sp6iad.superfeed.net...
>   - why is len() not a member function of strings? Instead one says 
> len(w).

Consider
>>> map(len, ('abc', (1,2,3), [1,2], {1:2}))
[3, 3, 2, 1]

Now try to rewrite this using methods (member functions).

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

Because it is an in-place mutation method and Guido decided that such 
methods should return None rather that the mutated object to lessen bugs. 
It is a tradeoff that has been debated ever since ;-)

>   For example: to acculumate words in a dictionary -
>        dict[key] += [word]

This expands to dict[key] = dict[key] + [word].  When the latter is valid 
(when dict[key] exists and is a list), then the former works fine also.

>>> k='a'
>>> d = {k:[]}
>>> d[k]+=['b']
>>> d
{'a': ['b']}

I presume the new-in-2.5 default dicts will do the same, and also work when 
the key does not exist and the default is a list.

Terry Jan Reedy






More information about the Python-list mailing list