Pythonic way for missing dict keys
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Sun Jul 22 05:19:59 EDT 2007
On Sat, 21 Jul 2007 16:20:37 -0700, genro wrote:
> On Jul 19, 6:29 am, Bruno Desthuilliers
> <bdesth.quelquech... at free.quelquepart.fr> wrote:
>> 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())
Reduces the unnecessary instantiation of `myobject` to "false" objects.
May be not good enough.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list