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

Paul Rubin http
Mon Jan 14 21:08:52 CET 2008


aspineux <aspineux at gmail.com> writes:
> 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 ?

Changing python semantics for something like this is nuts.  Allowing
passing a callable (sort of like re.sub allows) makes a certain
amount of sense:

   d.get('a', default=f)

You can also write (python 2.5, untested):

   d['a'] if 'a' in d else f()



More information about the Python-list mailing list