Default attribute values pattern

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

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

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


More information about the Python-list mailing list