diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-12-07 05:17:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-07 05:17:09 (GMT) |
commit | facca72eae3c0b59b4e89bab81c936dff10fb58a (patch) | |
tree | 3bd10a9b9904b98b84052d46d88698e392539548 | |
parent | 2a9a883d361d37b40fb0b0011dd300bb83ceb73c (diff) | |
download | cpython-facca72eae3c0b59b4e89bab81c936dff10fb58a.zip cpython-facca72eae3c0b59b4e89bab81c936dff10fb58a.tar.gz cpython-facca72eae3c0b59b4e89bab81c936dff10fb58a.tar.bz2 |
bpo-38843: Document behavior of default when the attribute is already set (GH-23653) (#23668)
(cherry picked from commit 752cdf21eb2be0a26ea6a34a0de33a458459aead)
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
-rw-r--r-- | Doc/library/argparse.rst | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index a32b999..02cd70f 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -696,7 +696,7 @@ The add_argument() method * const_ - A constant value required by some action_ and nargs_ selections. * 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. @@ -1006,6 +1006,14 @@ was not present at the command line:: >>> parser.parse_args([]) 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 were a command-line argument. In particular, the parser applies any type_ conversion argument, if provided, before setting the attribute on the |