unexpected optparse set_default/set_defaults behavior

mbeachy at gmail.com mbeachy at gmail.com
Fri Aug 17 16:12:01 CEST 2007

Some rather unexpected behavior in the set_default/set_defaults
methods for OptionParser that I noticed recently:

>>> import optparse
>>> parser = optparse.OptionParser()
>>> parser.add_option("-r", "--restart", dest="restart", action="store_true")
<Option at 0x-483b3414: -r/--restart>
>>> parser.defaults
{'restart': None}
>>> parser.set_default("retart", False)
>>> parser.defaults
{'retart': False, 'restart': None}

Why does set_default not raise an exception when passed a key that it
doesn't recognize?

Bad typysts bewaer.

The only reason I can think not to raise an exception is so that
defaults can be defined before the options are added. Is there some
use case that I'm not thinking of here? I realize that changing this
could break some existing scripts, but I'm still tempted to file this
as a bug.


More information about the Python-list mailing list