Default attribute values pattern

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Jan 21 13:19:59 EST 2008


cokofreedom at gmail.com a écrit :
>> Grab(argdict, key, default) is argdict.pop(key, default)
> 
> "pop() raises a KeyError when no default value is given and the key is
> not found."

Then use it with a default value !-)

>> def grab(kw, key, default=None):
>>    try:
>>      return kw.pop(key)
>>    except KeyError:
>>      return default
> 
> So Bruno's technique seems to me to be the correct one as it catches
> the KeyError.

Note that I cancelled the message (too bad, doesn't work everywhere). I 
totally agree with Arnaud on this, and should sometimes re-read the doc 
for stuff I use so often I think I know them.



More information about the Python-list mailing list