[New-bugs-announce] [issue39173] _AttributeHolder of argparse should support the sort function or not?

hai shi report at bugs.python.org
Tue Dec 31 10:23:53 EST 2019


New submission from hai shi <shihai1991 at 126.com>:

Currently, many developers discuss the output of attributes of argparse should be sorted or not?

>>> from argparse import ArgumentParser
>>> parser = ArgumentParser()
>>> _ = parser.add_argument('outstream')
>>> _ = parser.add_argument('instream')
>>> args = parser.parse_args(['out.txt', 'in.txt'])

# Keep the original order
>>> vars(args)

{'outstream': 'out.txt', 'instream': 'in.txt'}
# Order is sorted
>>> args
Namespace(instream='in.txt', outstream='out.txt')

IMHO, the attributes order should be keep the original order by default. If user would like use order the attributes order, we should add a param in `_AttributeHolder` to open sorting or not.

such as:
```
class _AttributeHolder(object):
    def __init__(self, sort=false):
        self.sort = sort

    def _get_kwargs(self):
        if sort:
            return sorted(self.__dict__.items())
        else:
            return self.__dict__.items()
```

some other bpos have discussed this topic too: issue39075、issue39058

----------
components: Library (Lib)
messages: 359118
nosy: shihai1991
priority: normal
severity: normal
status: open
title: _AttributeHolder of argparse should support the sort function or not?
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39173>
_______________________________________


More information about the New-bugs-announce mailing list