short path evaluation, why is f() called here: dict(a=1).get('a', f())

aspineux aspineux at gmail.com
Tue Jan 15 09:02:48 EST 2008


On Jan 15, 12:15 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Steven D'Aprano <st... at REMOVE-THIS-cybersource.com.au> writes:
> > map = {'a': Aclass, 'b': Bclass, 'c': Cclass}
> > class_ = map.get(astring, default=Zclass)
>
> > The result I want is the class, not the result of calling the class
> > (which would be an instance). If I wanted the other semantics, I'd be
> > using defaultdict instead.
>
> I used default as a keyward arg name indicating the presence of
> a callable.  I probably should have called it defaultfunc or something.
>
> x = d.get('a', f)      # --> default value is f
> x = d.get('a', defaultfunc=f)  # --> default value is result of f() .

Nice idea, but if I want args I need to write it like that:

x=d.get('a', defaultfunc=f, funcargs=(1,2,3))

instead of d['a', f(1,2,3)]




More information about the Python-list mailing list