[Python-Dev] Proposal: defaultdict

Ian Bicking ianb at colorstudy.com
Sat Feb 18 00:21:52 CET 2006

Martin v. Löwis wrote:
>>I know *I* at least don't like code that mixes up access and 
>>modification.  Maybe not everyone does (or maybe not everyone thinks of 
>>getitem as "access", but that's unlikely).  I will assert that it is 
>>Pythonic to keep access and modification separate, which is why methods 
>>and attributes are different things, and why assignment is not an 
>>expression, and why functions with side effects typically return None, 
>>or have names that are very explicit about the side effect, with names 
>>containing command verbs like "update" or "set".  All of these 
>>distinguish access from modification.
> Do you never write
>  d[some_key].append(some_value)
> This is modification and access, all in a single statement, and all
> without assignment operator.

(d[some_key]) is access.  (...).append(some_value) is modification. 
Expressions are compound; of course you can mix both access and 
modification in a single expression.  d[some_key] is access that returns 
something, and .append(some_value) modifies that something, it doesn't 
modify d.

> I don't see the setting of the default value as a modification.
> The default value has been there, all the time. It only is incarnated
> lazily.

It is lazily incarnated for multidict, because there is no *noticeable* 
side effect -- if there is any internal side effects that is an 
implementation detail.  However for default_factory=list, the result of 
.keys(), .has_key(), and .items() changes when you do d[some_key].

Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org

More information about the Python-Dev mailing list