short path evaluation, why is f() called here: dict(a=1).get('a', f())
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Jan 14 16:38:17 EST 2008
On Mon, 14 Jan 2008 12:08:52 -0800, Paul Rubin wrote:
> 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)
But how can Python determine when you want the result to be *the
callable* and when you want it to be *the result of calling the
callable*?
Functions and other callables are first-class objects, and it is quite
reasonable to have something like this:
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.
--
Steven
More information about the Python-list
mailing list