dictionary wart

Paul Rubin http
Thu Mar 18 14:10:59 CET 2004


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.



More information about the Python-list mailing list