[issue39076] Use types.SimpleNamespace for argparse.Namespace
Raymond Hettinger
report at bugs.python.org
Wed Dec 18 00:42:15 EST 2019
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
There are some sticking points:
* types.SimpleNamespace() sorts attributes, so this would get in the way of issue #39058.
* argparse.Namespace() supports a __contains__() method that isn't offered by types.SimpleNamespace():
>>> 'abc' in Namespace(abc=10)
True
* Argparse is sensitive to start-up time so we mostly want to avoid adding new dependencies. Start-up time recently got worse when the re module added a number of dependencies.
* The __repr__ for SimpleNamespace() doesn't round-trip and isn't what we have now with Namespace. That potentially breaks doctests and diverges from published examples:
>>> Namespace(abc=10)
Namespace(abc=10)
>>> SimpleNamespace(abc=10)
namespace(abc=10)
* Ironically, the class name "Namespace" is simpler than "SimpleNamespace" ;-)
* Much of the code in argparse.Namespace() inherits from _AttributeHolder, so switching to types.SimpleNamespace() doesn't really save us much code.
Are there any upsides to switching? Attribute lookup is almost equally fast using either approach, so there is no speed benefit:
$ python3.8 -m timeit -r 11 -s 'from argparse import Namespace as NS' -s 'args=NS(abc=10)' 'args.abc'
10000000 loops, best of 11: 32.7 nsec per loop
$ python3.8 -m timeit -r 11 -s 'from types import SimpleNamespace as NS' -s 'args=NS(abc=10)' 'args.abc'
10000000 loops, best of 11: 32.4 nsec per loop
----------
assignee: -> rhettinger
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39076>
_______________________________________
More information about the Python-bugs-list
mailing list