So the Zen says that the most (or the least) important part of philosophy is:
“Namespaces are one honking great idea – let’s do more of those!” - I appreciate that this isn’t exactly what this quote is about, but still…
Why do I like them? Why not just dict
?
- Usual design considerations
- It has ~3x faster access. The way it currently is in Python - namespace (or instance attribute access) is a
Mapping
with access speed similar to the one ofSequence
.
Motivation
Although much less used than say dict
, but namespaces are still widely used and are implemented in many different places across CPython
:
types.SimpleNamespace
argparse.Namespace
multiprocessing.dummy.Namespace
multiprocessing.managers.Namespace
Github search shows quite a lot of repetition of very similar class implementations:
/class Namespace(\((object)?\))?:/ Language:Python
- 5.6 k files link
Not all of these are true positives, but a significant part seems to be. Even if some of the cases need different functionality, inheriting from one-go-to class would already be better.
I have written and have one for my own use.
Main reason is that I am not happy with the selection:
types.SimpleNamespace
is slower thanargparse.Namespace
. It requires import (same as others), but furthermore verbosity of it is highly unattractive.argparse.Namespace
lives in location which does not suggest that it is a good place to use namespace from.
Proposal
In [20]: types.SimpleNamespace(a=1)
Out[20]: namespace(a=1) <- this is pleasant
So builtins.namespace
:
- Convenient placement
- Optimal performance implementation
- Reasonable length class name
I think this would save a fair amount of repetition if it was available as conveniently as dict
.
Furthermore, some convenient features could be added later to it. E.g.
__getitem__
access - there are many libraries that added attribute access todict
(such asdotwiz
), but all of the attempts that I have seen had to sacrifice a lot of performance. And also, it does not make sense to add attribute access todict
as__getattr__
possible keys is a subset of__getitem__
keys. Thus, doing the other way round might make more sense.__iter__
convenience method toiter(vars(namespace))
.
But these are just further considerations.
The 1st question is whether others have a similar view regarding improvement of this situation to myself and whether this deserves further consideration in the first place.
In Short
Would namespace
type fit next to list
, dict
and other builtins, both in placement and implementation regards?