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

Skip to content

Commit 752cdf2

Browse files
authored
bpo-38843: Document behavior of default when the attribute is already set (GH-23653)
1 parent 8a62887 commit 752cdf2

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Doc/library/argparse.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ The add_argument() method
696696
* const_ - A constant value required by some action_ and nargs_ selections.
697697

698698
* default_ - The value produced if the argument is absent from the
699-
command line.
699+
command line and if it is absent from the namespace object.
700700

701701
* type_ - The type to which the command-line argument should be converted.
702702

@@ -1006,6 +1006,14 @@ was not present at the command line::
10061006
>>> parser.parse_args([])
10071007
Namespace(foo=42)
10081008

1009+
If the target namespace already has an attribute set, the action *default*
1010+
will not over write it::
1011+
1012+
>>> parser = argparse.ArgumentParser()
1013+
>>> parser.add_argument('--foo', default=42)
1014+
>>> parser.parse_args([], namespace=argparse.Namespace(foo=101))
1015+
Namespace(foo=101)
1016+
10091017
If the ``default`` value is a string, the parser parses the value as if it
10101018
were a command-line argument. In particular, the parser applies any type_
10111019
conversion argument, if provided, before setting the attribute on the

0 commit comments

Comments
 (0)