Default attribute values pattern

Arnaud Delobelle arnodel at googlemail.com
Mon Jan 21 12:33:28 CET 2008


On Jan 21, 10:09 am, cokofree... at gmail.com wrote:
> > 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."

And it doesn't if a default is provided, which is always the case in
the uses of Grab(...), so it seems the right tool for the job.

> > 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.

If you really really want to write a grab function (IMHO pop is good
enough):

def grab(kw, key, default=None):
    return kw.pop(key, default)

--
Arnaud



More information about the Python-list mailing list