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

Skip to content

Commit d09a05e

Browse files
committed
Merge 3.2 except the massive Misc/NEWS conflict, so hand apply the NEWS entry.
2 parents dbe5088 + 1dedd0a commit d09a05e

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

Doc/library/argparse.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,17 @@ was not present at the command line::
923923
>>> parser.parse_args(''.split())
924924
Namespace(foo=42)
925925

926+
If the ``default`` value is a string, the parser parses the value as if it
927+
were a command-line argument. In particular, the parser applies any type_
928+
conversion argument, if provided, before setting the attribute on the
929+
:class:`Namespace` return value. Otherwise, the parser uses the value as is::
930+
931+
>>> parser = argparse.ArgumentParser()
932+
>>> parser.add_argument('--length', default='10', type=int)
933+
>>> parser.add_argument('--width', default=10.5, type=int)
934+
>>> parser.parse_args()
935+
Namespace(length=10, width=10.5)
936+
926937
For positional arguments with nargs_ equal to ``?`` or ``*``, the ``default`` value
927938
is used when no command-line argument was present::
928939

@@ -961,6 +972,9 @@ types and functions can be used directly as the value of the ``type`` argument::
961972
>>> parser.parse_args('2 temp.txt'.split())
962973
Namespace(bar=<_io.TextIOWrapper name='temp.txt' encoding='UTF-8'>, foo=2)
963974

975+
See the section on the default_ keyword argument for information on when the
976+
``type`` argument is applied to default arguments.
977+
964978
To ease the use of various types of files, the argparse module provides the
965979
factory FileType which takes the ``mode=`` and ``bufsize=`` arguments of the
966980
:func:`open` function. For example, ``FileType('w')`` can be used to create a

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ Build
9999
Documentation
100100
-------------
101101

102+
- Issue #15935: Clarification of argparse docs, re: add_argument() type and
103+
default arguments. Patch contributed by Chris Jerdonek.
104+
102105
- Issue #11964: Document a change in v3.2 to the behavior of the indent
103106
parameter of json encoding operations.
104107

0 commit comments

Comments
 (0)