bpo-38843: Document behavior of default when the attribute is already set (GH-23653)

This commit is contained in:
Raymond Hettinger 2020-12-06 18:29:08 -08:00 committed by GitHub
parent 8a62887dfb
commit 752cdf21eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -696,7 +696,7 @@ The add_argument() method
* const_ - A constant value required by some action_ and nargs_ selections. * const_ - A constant value required by some action_ and nargs_ selections.
* default_ - The value produced if the argument is absent from the * default_ - The value produced if the argument is absent from the
command line. command line and if it is absent from the namespace object.
* type_ - The type to which the command-line argument should be converted. * type_ - The type to which the command-line argument should be converted.
@ -1006,6 +1006,14 @@ was not present at the command line::
>>> parser.parse_args([]) >>> parser.parse_args([])
Namespace(foo=42) Namespace(foo=42)
If the target namespace already has an attribute set, the action *default*
will not over write it::
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default=42)
>>> parser.parse_args([], namespace=argparse.Namespace(foo=101))
Namespace(foo=101)
If the ``default`` value is a string, the parser parses the value as if it If the ``default`` value is a string, the parser parses the value as if it
were a command-line argument. In particular, the parser applies any type_ were a command-line argument. In particular, the parser applies any type_
conversion argument, if provided, before setting the attribute on the conversion argument, if provided, before setting the attribute on the