Pythonic way for missing dict keys
giovanni.porcari at gmail.com
Sat Jul 21 19:20:37 EDT 2007
On Jul 19, 6:29 am, Bruno Desthuilliers
<bdesth.quelquech... at free.quelquepart.fr> wrote:
> Myobject will be instanciated each time, yes.
> > and so if the initialization is expensive you
> > will probably see surprises.
> No "surprise" here, but it can indeed be suboptimal if instanciating
> myobject is costly.
What about this way ?
my_obj = my_dict.get(key) or my_dict.setdefault(key,myobject())
More information about the Python-list