Text of discuss.python.org › t › add-builtins-namespace › 78343 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Add `builtins.namespace` - Ideas - Discussions on Python.org

Add `builtins.namespace` Ideas dg-pb (dg-pb) January 26, 2025, 1:23am 1 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 of Sequence .

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 than argparse.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 to dict (such as dotwiz ), 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 to dict as __getattr__ possible keys is a subset of __getitem__ keys. Thus, doing the other way round might make more sense. __iter__ convenience method to iter(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? 1 Like AA-Turner (Adam Turner) January 26, 2025, 1:37am 2 Please could you elaborate on this point? I’m a fan of types.SimpleNamespace and frequently use e.g. for a simple mocks in a test, or when prototyping, but I haven’t had any cause to benchmark it. A 1 Like Update() method for SimpleNamespace dg-pb (dg-pb) January 26, 2025, 1:54am 3 class ClassNamespace: def __init__(self, **kwds): for k, v in kwds.items(): setattr(self, k, v) ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ macOS-11.7.10-x86_64-i386-64bit | CPython: 3.12.4 ┃ ┃ 5 repeats, 1,000,000 times ┃ ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ ┃ Units: ns set get ┃ ┃ ┏━━━━━━━━━━━━━━━━━━━━━┫ ┃ SimpleNamespace ┃ 49 35 ┃ ┃ argp.Namespace ┃ 15 11 ┃ ┃ ClassNamespace ┃ 8 7 ┃ ┃ namedtuple ┃ - 26 ┃ ┃ list ┃ 10 8 ┃ ┃ tuple ┃ - 9 ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━┛ csm10495 (Charles Machalow) January 26, 2025, 1:56am 4 I’m a -1 since we already have enough similar looking things.

Besides the one you mentioned we also have dataclasses.dataclass which is arguably similar. Then we also have collections.namedtuple which is once again arguably the same thing from the user’s perspective. If we’re bothered about the performance of imports (which is a common trend I see around here), we should continue to look for optimizations in that route instead of trying to make things not need imports. Interestingly copy/pasting that search query into github search found me nothing ( link ): image 1160×378 46.8 KB (though I must be doing something wrong to find nothing with it).

Also quoting the zen: There should be one-- and preferably only one --obvious way to do it. We already have too many ways for this and I don’t think its useful enough (in a way that it is more useful than just using an existing option) to be in builtins. dg-pb (dg-pb) January 26, 2025, 2:02am 5 My bad, I have only written regex, not the full query. Updated in the main post as: Github search shows quite a lot of repetition of very similar class implementations: /class Namespace(\((object)?\))?:/ Language:Python - 5.6 k files Try this Well, this is kind of my main point, that there is no one obvious way.

But yes, on the flip side, the cost of introducing such is non-negligible. In the short term this would be costly and it is expected that many would deem this unnecessary. I am just thinking whether this would pay off In longer term. 1 Like Rosuav (Chris Angelico) January 26, 2025, 2:06am 6 I prefer the name SimpleNamespace for the class, since “namespace” is a very generic term that covers all kinds of things (some of which are backed by dictionaries, others are not).

I’m not sure there’s enough need to have this in the builtins, but OTOH, types.SimpleNamespace isn’t all that good either; the one-line summary of the types module doesn’t suggest that you can find a thing here for providing attributes with dot notation. Maybe the types module needs a bit of a rebrand? 1 Like csm10495 (Charles Machalow) January 26, 2025, 2:06am 7 Thanks for the updated search. That makes more sense.

I figure there are a lot of red herrings in there. Though the ‘similar to existing solutions’ version of a namespace may be either old code or just folks unaware of similar things. Too bad things like linters can’t warn people that Something similar to <line xyz> exists in the stdlib as <thing.thang>. Consider using that instead of rewriting it here. Then at least they have to acknowledge it and # type ignore the line to get the linter to go away.

1 Like mikeshardmind (Michael H) January 26, 2025, 2:09am 8 dataclasses tend to be pretty slow to access attributes (slower than NamedTuples, which use _collections.tuplegetter to be faster), and are pretty slow just to import too since they use inspect currently. A lot of the “existing ways” really are worse in some use cases, even when presented as a modern solution. That said, I don’t know that adding builtins.namespace is really the right solution, we could just look for ways to make the other namespace-like things performant, possibly even using the same backing implementation if warranted, but leave dataclasses and types.SimpleNamespace as the obvious places to get that functionality, based on whether you just want a container, or the full trappings that come with dataclasses 1 Like AA-Turner (Adam Turner) January 26, 2025, 2:10am 9 Caveat: Microbenchmarks are not representative, etc: Using IPython for the %timeit command: from types import SimpleNamespace from argparse import Namespace as ArgparseNamespace class ClassSetattr: def __init__(self, **kwds): for k, v in kwds.items(): setattr(self, k, v) class ClassVars: def __init__(self, **kwds): vars(self).update(kwds) for name in 'SimpleNamespace', 'ArgparseNamespace', 'ClassSetattr', 'ClassVars': container = globals()[name] print('create', name) %timeit ns = container(a=

The rest of this page is on the live site.