dictionary wart

Jesper jolsen at mail2world.com
Thu Mar 18 08:46:19 EST 2004


Paul Rubin wrote:
> jolsen at mail2world.com (Jesper Olsen) writes:
> 
>>Does python have a way of defining a dictionary default? 
>>I think not, but are there any plans to incorporate it?
>>
>>Intuitively I would imagine that
>>
>>a={}
>>a.set_default(my_default)
>>
>>would do this -ie. a[my_new_key] should now return the the default
>>value my_default instead of creating an exception. 
> 
> 
> No, instead of a[my_new_key] use a.get(my_new_key, my_default).
> That does what you want.

Unfortunately not - get() is different, because you can not assign to it.
For instance in a dictionary which is mapping strings to integers (or 
lists), I would like to do

a[my_key]+=5

expressing that with get() would be awkward.

Jesper



More information about the Python-list mailing list