short path evaluation, why is f() called here: dict(a=1).get('a', f())
Neil Cerutti
mr.cerutti at gmail.com
Mon Jan 14 13:53:51 EST 2008
On Jan 14, 2008 1:39 PM, aspineux <aspineux at gmail.com> wrote:
>
> This append in both case
>
> dict(a=1).get('a', f())
> dict(a=1).setdefault('a', f())
>
> This should be nice if f() was called only if required.
Shortcomings of those methods is probably why collections.defaultdict
is so popular.
>>> def f():
... return 7
...
>>> d = defaultdict(f, a=1)
>>> d['a']
1
>>> d['b']
7
get and setdefault aren't needed when using a default dict, and the
default factory is called only when needed.
--
Neil Cerutti <mr.cerutti+python at gmail.com>
More information about the Python-list
mailing list