short path evaluation, why is f() called here: dict(a=1).get('a', f())
aspineux at gmail.com
Tue Jan 15 15:17:57 CET 2008
On Jan 14, 8:07 pm, aspineux <aspin... at gmail.com> wrote:
> On Jan 14, 7:49 pm, "Chris Mellon" <arka... at gmail.com> wrote:
> > On Jan 14, 2008 12:39 PM, aspineux <aspin... 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.
> > Think about the change to Python semantics that would be required for
> > this to be true, and then use collections.defaultdict instead.
> Yes, I missed 'get' and 'setdefault' are functions :-)
> Then why not some new semantic
> d.get('a', f()) --> d['a', f()]
> d.setdefault('a', f()) --> d['a'=f()]
> Is is a good idea enough to change the python semantic ?
> Or simply is it a good idea ?
Thanks for all your answers.
Anyway these notations are very compact,
don't require the definition of a specific function,
and work with old style/or already existing dictionary,
dictionary you didn't created yourself.
While the use of defaultdict require the definition of such a function
and to control the creation of the dictionary.
For me the best alternative that match the requirement above is
the one provided by Paul Rubin.
More information about the Python-list