Thanks to visit codestin.com
Credit goes to github.com

Skip to content

argparse set_defaults  #108009

Open
@daggaz

Description

@daggaz

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

Labels

docsDocumentation in the Doc dir

Projects

Status

Doc issues

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions