# Namespace blocks

**URL:** <https://discuss.python.org/t/namespace-blocks/108875>\
**Category:** Ideas\
**Created:** [September 3, 2026, 1:54pm UTC](https://discuss.python.org/t/namespace-blocks/108875 "2026-09-03T13:54:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![BenjyWiener](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/benjywiener/32/33309_2.png) [@BenjyWiener](https://discuss.python.org/u/BenjyWiener)\
**Post date:** [September 3, 2026, 1:54pm UTC](https://discuss.python.org/t/namespace-blocks/108875/1 "2026-09-03T13:54:20Z")

</div>

I started playing with the idea of namespaces in Python due to the recent discussions about `public`/`private`.

It’s often useful to group related items under a name, but creating a submodule isn’t always desirable.

Python currently has two constructs that introduce a new namespace (and scope), `class` and `def`.  
`class` is fine for grouping constants or even classes, but due to Python’s name resolution rules, not for functions.  
On the other hand, functions are obviously not evaluated immediately. This can be solved with a simple decorator, but retrieving the new namespace requires either an explicit `return locals()` or some fragile reflection. Additionally, classes and methods defined in a function get `.<locals>.` in their ` __qualname__ `.

I’d like to propose a new general-purpose `namespace` block:

```python
namespace consts:
    X = 1
    Y = 2

# Name can be omitted to create a simple lexical scope,
# useful to prevent littering the outer scope.
namespace:
    global x
    y = ...
    z = ...
    x = y + z
# y and z are undefined here

```

Name resolution inside a namespace is similar to functions:

```python
namespace a:
    x = 1
    f: Callable[[], int]
    namespace b:
        a.x += 1
        y = 2

        nonlocal f
        def f() -> int:
            return a.x + y

```

Private namespaces:

```python
# mod.py
namespace __internal__ :
    from ._private_utils import my_util

    def private_func(): ...

    global public_func
    def public_func():
        return my_util(...)

```

```python
# script.py
import mod
assert mod.public_func. __qualname__ == 'mod.public_func'
# Internal names are still accessible for monkey-patching and experimentation
assert mod. __internal__.private_func. __qualname__ == 'mod. __internal__.private_func'

```

When used as a scope, `namespace` can be useful for cleaning up intermediate variables that would otherwise live forever (or until the end of a function):

```python
namespace:
    # No need to `del big_object1, big_object2`, they're refcounts will be zero
    # after exiting this anonymous namespace.
    big_object1 = json.loads(big_json.read_string())
    big_object2 = json.loads(big_json.read_string())
    global x, y
    x = big_object1['x']
    y = big_object2['y']

```

---

<div class="post-metadata">

**Author:** ![duncathan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/duncathan/32/25521_2.png) [@duncathan](https://discuss.python.org/u/duncathan)\
**Post date:** [September 3, 2026, 2:19pm UTC](https://discuss.python.org/t/namespace-blocks/108875/2 "2026-09-03T14:19:16Z")

</div>

> [@BenjyWiener](#):
>
> `class` is fine for grouping constants or even classes, but due to Python’s name resolution rules, not for functions.

Could you expand on this? Is it not sufficient to decorate a function with `@staticmethod`?

---

<div class="post-metadata">

**Author:** ![warden](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@warden](https://discuss.python.org/u/warden)\
**Post date:** [September 3, 2026, 2:31pm UTC](https://discuss.python.org/t/namespace-blocks/108875/3 "2026-09-03T14:31:10Z")

</div>

I have the same question and am probably missing something, but why even bother with staticmethod?

```python
class namespace:
    def double(value):
        return 2*value

namespace.double("abc") # "abcabc"
namespace.double. __qualname__ # "namespace.double"

```

---

<div class="post-metadata">

**Author:** ![tmk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tmk/32/10935_2.png) [@tmk](https://discuss.python.org/u/tmk)\
**Post date:** [September 3, 2026, 5:39pm UTC](https://discuss.python.org/t/namespace-blocks/108875/4 "2026-09-03T17:39:26Z")

</div>

I think this is a great idea and I have wished for this before.

Regarding whether you can just use `class`: maybe you technically can, but it looks strange to have procedural code in a class and it just makes the intent clearer to use a `namespace`. Also makes it clear you’re not supposed to instantiate this namespace as an object (the way you can do with a class).

I think ideally the namespace would just behave as a submodule. So

```python
# a.py

a = 1

namespace b:
  b = 1

```

is equivalent to

```python
# a/ __init__.py

a = 1

# a/b.py

b = 1

```

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 3, 2026, 5:49pm UTC](https://discuss.python.org/t/namespace-blocks/108875/5 "2026-09-03T17:49:32Z")

</div>

Because that version is missing the `self` parameter (and would fail with the additional input you give it).

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 3, 2026, 5:50pm UTC](https://discuss.python.org/t/namespace-blocks/108875/6 "2026-09-03T17:50:15Z")

</div>

This is a pretty different proposal though, in that there’s no scope effects.

---

<div class="post-metadata">

**Author:** ![syntaxfiend](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@syntaxfiend](https://discuss.python.org/u/syntaxfiend)\
**Post date:** [September 3, 2026, 5:58pm UTC](https://discuss.python.org/t/namespace-blocks/108875/7 "2026-09-03T17:58:01Z")

</div>

not exactly. You don’t need a self parameter. If you access the function from the class then self is not automatically given to the function as it would be when calling it from an instance of a class.

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [September 3, 2026, 6:03pm UTC](https://discuss.python.org/t/namespace-blocks/108875/8 "2026-09-03T18:03:47Z")

</div>

Somewhat related, there’s a pre-draft PEP for a `make` statement that could be used for this:

> <https://github.com/python/peps/pull/4974>
>
> \## Basic requirements (all PEP Types)
> 
> \* \[\] Read and followed \[PEP 1\](https:/…/peps.python.org/1) & \[PEP 12\](https://peps.python.org/12)
> \* \[\] File created from the \[latest PEP template\](https://github.com/python/peps/blob/main/peps/pep-0012/pep-NNNN.rst?plain=1)
> \* \[\] PEP has next available number, & set in filename (\`\`pep-NNNN.rst\`\`), PR title (\`\`PEP 123: \<Title of PEP\>\`\`) and \`\`PEP\`\` header
> \* \[\] Title clearly, accurately and concisely describes the content in 79 characters or less
> \* \[\] Core dev/PEP editor listed as \`\`Author\`\` or \`\`Sponsor\`\`, and formally confirmed their approval
> \* \[\] \`\`Author\`\`, \`\`Status\`\` (\`\`Draft\`\`), \`\`Type\`\` and \`\`Created\`\` headers filled out correctly
> \* \[\] \`\`PEP-Delegate\`\`, \`\`Topic\`\`, \`\`Requires\`\` and \`\`Replaces\`\` headers completed if appropriate
> \* \[\] Required sections included
> \* \[\] Abstract (first section)
> \* \[\] Copyright (last section; exact wording from template required)
> \* \[\] Code is well-formatted (PEP 7/PEP 8) and is in \[code blocks, with the right lexer names\](https://peps.python.org/pep-0012/#literal-blocks) if non-Python
> \* \[\] PEP builds with no warnings, pre-commit checks pass and content displays as intended in the rendered HTML
> \* \[\] Authors/sponsor added to \`\`.github/CODEOWNERS\`\` for the PEP
> 
> 
> \## Standards Track requirements
> 
> \* \[\] PEP topic \[discussed in a suitable venue\](https://peps.python.org/pep-0001/#start-with-an-idea-for-python) with general agreement that a PEP is appropriate
> \* \[\] \[Suggested sections\](https://peps.python.org/pep-0012/#suggested-sections) included (unless not applicable)
> \* \[\] Motivation
> \* \[\] Specification
> \* \[\] Rationale
> \* \[\] Backwards Compatibility
> \* \[\] Security Implications
> \* \[\] How to Teach This
> \* \[\] Reference Implementation
> \* \[\] Rejected Ideas
> \* \[\] Open Issues
> \* \[\] Acknowledgements
> \* \[\] Footnotes
> \* \[\] Change History
> \* \[\] \`\`Python-Version\`\` set to valid (pre-beta) future Python version, if relevant
> \* \[\] Any project stated in the PEP as supporting/endorsing/benefiting from the PEP formally confirmed such
> \* \[\] Right before or after initial merging, \[PEP discussion thread\](https://peps.python.org/pep-0001/#discussing-a-pep) created and linked to in \`\`Discussions-To\`\` and \`\`Post-History\`\`

The actual PEP is a bit more general, but one of the examples shows how it could be used to build simple namespaces:

```python
namespace = ... # <setup omitted>

make namespace Constants:
    AIRSPEED_AFRICAN = 5
    AIRSPEED_EUROPEAN = 8

```

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 3, 2026, 6:15pm UTC](https://discuss.python.org/t/namespace-blocks/108875/9 "2026-09-03T18:15:14Z")

</div>

huh TIL. I was thinking of an instance of the class

---

<div class="post-metadata">

**Author:** ![BenjyWiener](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/benjywiener/32/33309_2.png) [@BenjyWiener](https://discuss.python.org/u/BenjyWiener)\
**Post date:** [September 3, 2026, 7:25pm UTC](https://discuss.python.org/t/namespace-blocks/108875/10 "2026-09-03T19:25:14Z")

</div>

Name resolution for methods skips the containing class(es).

Take this example:

```python
class MyNamespace:
    x = 1
    def f():
        return x # NameError: name 'x' is not defined

```

You would need to use `MyNamespace.x`, which is cumbersome, especially with more than one nesting level.

With `namespace`, `f` would close over `x`.

---

<div class="post-metadata">

**Author:** ![BenjyWiener](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/benjywiener/32/33309_2.png) [@BenjyWiener](https://discuss.python.org/u/BenjyWiener)\
**Post date:** [September 3, 2026, 7:30pm UTC](https://discuss.python.org/t/namespace-blocks/108875/11 "2026-09-03T19:30:58Z")

</div>

> [@tmk](#):
>
> I think ideally the namespace would just behave as a submodule.

That’s roughly the idea, but I chose to not phrase it that way because modules are heavily linked to the import system. My proposal wouldn’t allow `import mod.namespace`, nor would it appear in `sys.modules`.

---

<div class="post-metadata">

**Author:** ![BenjyWiener](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/benjywiener/32/33309_2.png) [@BenjyWiener](https://discuss.python.org/u/BenjyWiener)\
**Post date:** [September 3, 2026, 7:54pm UTC](https://discuss.python.org/t/namespace-blocks/108875/12 "2026-09-03T19:54:58Z")

</div>

Thanks for the link! It seems that @Jelle’s proposal would have the same name resolution issue as `class` though.

---

<div class="post-metadata">

**Author:** ![syntaxfiend](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@syntaxfiend](https://discuss.python.org/u/syntaxfiend)\
**Post date:** [September 4, 2026, 5:33pm UTC](https://discuss.python.org/t/namespace-blocks/108875/13 "2026-09-04T17:33:56Z")

</div>

I really like this idea, I hope we get something like this! I’ve been fighting python for the past day and a half trying to figure out something similar.

If they do go ahead with this I hope that we can parameterize the namespace to make it rebindable. Maybe that’s not the right terminology. but I’d like for it to take arguments. So that you can create shared functionality without having to deal with attribute lookup and self and wrapping 50 functions with staticmethod.

```python
namespace math_funcs(a):
    def add(x):
        return a + x
    def div(x):
        return a / x

math1 = math_funcs(1)
assert math1.add(1) == 2

math2 = math_funcs(2)
assert math2.div(4) == 0.5

```

classes are unnecessarily heavy and verbose for this. If you aren’t using actual OOP concepts like polymorphism and inheritance then I’d even go so far as to say that a class is an anti-pattern. It’s like buying a giant pickup truck when your only need for transportation is going to get groceries.

You could use a function but then you end up having to re-specify all the types and add everything to the return statement and that’s a big ol repetitive PITA. It would be like only having a bicycle for transportation when you live 25 miles from the grocery store.

So trying to use a class you need to strip unnecessary parts from it. and trying to use a function you have to try and reintroduce a subset of those parts on your own every time.

---

<div class="post-metadata">

**Author:** ![Nodd](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nodd/32/6805_2.png) [@Nodd](https://discuss.python.org/u/Nodd)\
**Post date:** [September 4, 2026, 7:31pm UTC](https://discuss.python.org/t/namespace-blocks/108875/14 "2026-09-04T19:31:43Z")

</div>

Well, one of the best point of computer science is that you can reuse the same vehicle for multiple different uses, rather than having a huge garage with one vehicule per travel distance. What you call an anti-pattern for class is just regular programming in my eyes.

Regarding the namespace proposal, it’s true that it seems nice, but at the same time I don’t think I would use it anywhere in my code, there are already so many tools to do a sufficiently equivalent result.

---

<div class="post-metadata">

**Author:** ![syntaxfiend](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@syntaxfiend](https://discuss.python.org/u/syntaxfiend)\
**Post date:** [September 4, 2026, 8:47pm UTC](https://discuss.python.org/t/namespace-blocks/108875/15 "2026-09-04T20:47:12Z")

</div>

python added the matrix multiply operator `@` for scientific computing. I don’t use it in my code. That doesn’t mean it isn’t a valuable addition to the language.

This proposal would allow for solving problems in a more efficient manner. Looking up a closed over variable is more efficient than looking up an attribute. Creation of a namespace will almost certainly be more efficient than creating a class because it is a simpler tool.

Personally I think this would be a big win for the language, it would lead to objectively more efficient code and subjectively I think it would lead to cleaner more expressive and succinct syntax. I don’t think that the current tooling is equivalent or sufficient, but these are opinions, we may disagree that’s ok 🙂

---

<div class="post-metadata">

**Author:** ![tmk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tmk/32/10935_2.png) [@tmk](https://discuss.python.org/u/tmk)\
**Post date:** [September 4, 2026, 8:48pm UTC](https://discuss.python.org/t/namespace-blocks/108875/16 "2026-09-04T20:48:21Z")

</div>

> [@syntaxfiend](#):
>
> ```python
> namespace math_funcs(a):
> def add(x):
> return a + x
> def div(x):
> return a / x
> 
> math1 = math_funcs(1)
> assert math1.add(1) == 2
> 
> math2 = math_funcs(2)
> assert math2.div(4) == 0.5
> 
> ```

This is seems like the perfect use case for a class. What don’t you like about this?

```python
@dataclass(frozen=True, slots=True)
class math_funcs:
    a: float
    def add(self, x):
        return self.a + x
    def div(self, x):
        return self.a / x

math1 = math_funcs(1)
assert math1.add(1) == 2

math2 = math_funcs(2)
assert math2.div(4) == 0.5

```

---

<div class="post-metadata">

**Author:** ![duncathan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/duncathan/32/25521_2.png) [@duncathan](https://discuss.python.org/u/duncathan)\
**Post date:** [September 4, 2026, 9:16pm UTC](https://discuss.python.org/t/namespace-blocks/108875/17 "2026-09-04T21:16:58Z")

</div>

> [@syntaxfiend](#):
>
> Looking up a closed over variable is more efficient than looking up an attribute. Creation of a namespace will almost certainly be more efficient than creating a class because it is a simpler tool.

It’s hard to take this claim at face value without a reference implementation demonstrating performance gains.

---

<div class="post-metadata">

**Author:** ![syntaxfiend](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@syntaxfiend](https://discuss.python.org/u/syntaxfiend)\
**Post date:** [September 4, 2026, 9:29pm UTC](https://discuss.python.org/t/namespace-blocks/108875/18 "2026-09-04T21:29:41Z")

</div>

I appreciate the `frozen` and `slots` with your example. But basically there is no need for `self`. So why include something that there is no need for?

Yeah I could use static methods but it just feels wrong and takes up a lot of extra space if you’re using it a lot. and then I can’t have access to `a`. which we do have in the namespace example. So you either get rid of self and have no shared state or you have to add a pointless parameter to your def.

---

<div class="post-metadata">

**Author:** ![syntaxfiend](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@syntaxfiend](https://discuss.python.org/u/syntaxfiend)\
**Post date:** [September 4, 2026, 9:39pm UTC](https://discuss.python.org/t/namespace-blocks/108875/19 "2026-09-04T21:39:02Z")

</div>

The first part is true, that much is known. The second part yes I agree we need a reference implementation. But the thinking is that a namespace is a simpler thing. A class does nearly everything a namespace does but has the added complexity of creating instances.

initializing an instance of a class would more than likely be cheaper. but using the functions from a namespace would be cheaper than calling an instances method. or at least that’s what I’m thinking, definitely TBD.

---

<div class="post-metadata">

**Author:** ![syntaxfiend](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@syntaxfiend](https://discuss.python.org/u/syntaxfiend)\
**Post date:** [September 4, 2026, 9:42pm UTC](https://discuss.python.org/t/namespace-blocks/108875/20 "2026-09-04T21:42:35Z")

</div>

I think namespaces would be super useful with or without parameters. If it doesn’t have parameters it could just be wrapped in a function to accomplish the same thing. slightly bulkier but still much better than what we can do currently.

[Next page](https://discuss.python.org/t/namespace-blocks/108875.md?page=2)
