Author rhettinger
Recipients rhettinger
Date 2019-12-15.22:16:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1576448164.39.0.171350779582.issue39058@roundup.psfhosted.org>
In-reply-to
Content
Currently, Namespace() objects sort the attributes in the __repr__.  This is annoying because argument order matters and because everywhere else in the module we preserve order (i.e. users see help in the order that arguments are added).

Note, the docs do not promise that Namespace is displayed with a sort.  This is likely just an artifact of older dictionaries having arbitrary or randomised ordering.


>>> from argparse import ArgumentParser
>>> parser = ArgumentParser()
>>> _ = parser.add_argument('source')
>>> _ = parser.add_argument('destination')

# Order matters to the user inputing the arguments 
# (source must go first and destination must go last
>>> args = parser.parse_args(['input.txt', 'output.txt'])

# Order is preserved internally
>>> vars(args)
{'source': 'input.txt', 'destination': 'output.txt'}

# Despite this, the Namespace() repr alphabetizes the output
>>> args
Namespace(destination='output.txt', source='input.txt')

# Order is preserved in help()
>>> parser.parse_args(['-h'])       
usage: [-h] source destination

positional arguments:
  source
  destination

optional arguments:
  -h, --help   show this help message and exit
History
Date User Action Args
2019-12-15 22:16:04rhettingersetrecipients: + rhettinger
2019-12-15 22:16:04rhettingersetmessageid: <1576448164.39.0.171350779582.issue39058@roundup.psfhosted.org>
2019-12-15 22:16:04rhettingerlinkissue39058 messages
2019-12-15 22:16:04rhettingercreate