[Python-Dev] setdefault's second argument

Tim Peters tim.peters at gmail.com
Tue Aug 30 18:56:11 CEST 2005

> setdefault() described it as behaving like dict.get() but inserting the
> key if not found.


> Likewise, I found zero occurrences in the library, in my cumulative code
> base, and in the third-party packages on my system.

>> If there isn't a sane use case for leaving the second argument out,
>> I'd like to drop the possibility in P3K (assuming setdefault()
>> survives).

> Give a lack of legitimate use cases, do we have to wait to Py3.0?  It
> could likely be fixed directly and not impact any code that people care
> about.

That would be fine by me, but any change really requires a
deprecation-warning release first.

Dang!  I may have just found a use, in Zope's
lib/python/docutils/parsers/rst/directives/images.py (which is part of
docutils, not really part of Zope):

    figwidth = options.setdefault('figwidth')
    figclass = options.setdefault('figclass')
    del options['figwidth']
    del options['figclass']

I'm still thinking about what that's trying to do <0.5 wink>. 
Assuming options is a dict-like thingie, it probably meant to do:

    figwidth = options.pop('figwidth', None)
    figclass = options.pop('figclass', None)

David, are you married to that bizarre use of setdefault <wink>?

Whatever, I can't claim there are _no_ uses of 1-arg setdefault() in
the wild any more.

More information about the Python-Dev mailing list