Open
Description
Documentation
The documentation for set_defaults()
in the argparse
module states:
Note that parser-level defaults always override argument-level defaults
And gives the following snippet:
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default='bar')
>>> parser.set_defaults(foo='spam')
>>> parser.parse_args([])
Namespace(foo='spam')
However, swapping the order of add_argument()
and set_defaults()
gives a different result:
>>> parser = argparse.ArgumentParser()
>>> parser.set_defaults(foo='spam')
>>> parser.add_argument('--foo', default='bar')
>>> parser.parse_args([])
Namespace(foo='bar')
To me, the statement in the documentation implies that parser-level defaults take precedence over argument-level defaults, but that is not the case. In fact, they overwrite one another, and order is important.
Linked PRs
Metadata
Metadata
Assignees
Projects
Status
Doc issues