# Namespace

A namespace is a context for identifiers, a logical grouping of names used in a program in which, within the same context and scope, an identifier must uniquely identify an entity<sup>[1](https://developer.mozilla.org/en-US/docs/Glossary/Namespace)</sup>. The concept shows up all over computing: an operating-system directory serves as a namespace where every file and subdirectory carries a name unique within it, yet identical names may reappear in different subdirectories; meanwhile, in HTML, CSS, and languages built on XML, a namespace denotes the dialect—whether declared outright or merely implied—that an element or attribute is part of.<sup>[1](https://developer.mozilla.org/en-US/docs/Glossary/Namespace)</sup> In programming languages, namespaces exist chiefly to prevent name collisions between independently written code while keeping everyday names short.

| Key fact | Detail |
|---|---|
| Definition | A context for identifiers; within the same context and scope, an identifier must uniquely identify an entity<sup>[1](https://developer.mozilla.org/en-US/docs/Glossary/Namespace)</sup> |
| C++ adoption | The C++ standards committee accepted a proposal for a namespace mechanism in 1994, replacing the earlier practice of prefixing externally linked names<sup>[2](https://stroustrup.com/hopl2.pdf)</sup><sup> • </sup><sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1408.html)</sup> |
| Java packages | A package is a namespace that organizes a set of related classes and interfaces, conceptually similar to folders on a computer<sup>[4](https://docs.oracle.com/javase/tutorial/java/concepts/package.html)</sup> |
| Python namespace packages | PEP 420 introduced implicit namespace packages in Python 3.3; they have no `__init__.py`, no `__file__` attribute, and can be split across multiple directories<sup>[5](https://peps.python.org/pep-0420/)</sup> |
| Rust namespaces | Rust segregates names into separate namespaces by kind of entity, so the same name in different namespaces does not conflict<sup>[6](https://doc.rust-lang.org/reference/names/namespaces.html)</sup> |
| Security dimension | Name resolution indirection creates attack avenues; a prototype enforcing per-system-call invariants on the Linux file system namespace ran with less than 3% overhead<sup>[7](https://pure.psu.edu/en/publications/a-rose-by-any-other-name-or-an-insane-root-adventures-in-name-res/)</sup> |
| Current standardization | A C2y proposal (WG14 N3794) aims to add namespaces to C without implicit name mangling by utilizing prefixes<sup>[8](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3794.pdf)</sup> |

## Purpose and mechanism

The problem namespaces solve is collision between names chosen independently. Before C++ introduced namespaces, it was common practice to apply prefixes to all names with external linkage to avoid unpredictable collisions between application code and the libraries it linked to; the practice worked reasonably well when prefixes were long, but the prefix had to be written everywhere, including inside the library's own definitions<sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1408.html)</sup>. Namespaces replaced prefixing with qualification: they partition the set of all names so that qualification can be avoided for names defined in local or enclosing contexts, while using-declarations and using-directives allow selective unqualified use of names from other namespaces, combining convenience with protection<sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1408.html)</sup>.

A namespace is distinct from a scope, though the two interact. A scope is a textual region of a program where a namespace is directly accessible, in the wording of the Python tutorial; Python maintains separate namespaces for functions, modules, and classes, and the statement `del x` removes the binding of `x` from the namespace referenced by the local scope<sup>[9](https://docs.python.org/3/tutorial/classes.html)</sup>. Name resolution, the process of associating each reference with its intended declaration, underlies static checking, translation, and editor services, and is complicated by qualified references: resolving a qualified reference in Java requires first resolving the class or package name to a context and then resolving the member name within that context<sup>[10](https://web.cecs.pdx.edu/~apt/esop15.pdf)</sup>.

## Namespaces in specific languages

**C++.** The 1994 committee acceptance of the namespace proposal addressed a long-standing gap: C++, like C, had lacked an effective mechanism for expressing modularity above the level of the class and the file, which had produced a series of complaints about name space pollution<sup>[2](https://stroustrup.com/hopl2.pdf)</sup>. The mechanism was later refined; a 2002 committee paper proposed new namespace and lookup rules to fix problems with argument-dependent lookup, noting that for ordinary function calls the replacement of prefixing by namespaces failed to deliver the promised convenience and protection<sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1408.html)</sup>. The standard's unqualified lookup rules search enclosing namespaces in order, from the innermost block outward to the global scope<sup>[11](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4778.pdf)</sup>.

**Java.** A package is a namespace that organizes a set of related classes and interfaces, and the Java platform's class library, the API, is organized as a set of such packages<sup>[4](https://docs.oracle.com/javase/tutorial/java/concepts/package.html)</sup>. Package names map onto directories and class names onto file names, so the compiler can locate source code from the full name of a class<sup>[12](https://www.cs.odu.edu/~zeil/cs261/latest/Public/packages/index.html)</sup>. Package names beginning with the identifier `java` are reserved for the platform's own packages<sup>[13](https://docs.oracle.com/javase/specs/jls/se6/html/names.html)</sup>.

**Python.** Every module, class, and function body has a namespace, and packages organize modules hierarchically with dot-separated names such as `email.mime.text`<sup>[9](https://docs.python.org/3/tutorial/classes.html)</sup><sup> • </sup><sup>[14](https://docs.python.org/3.12/reference/import.html)</sup>. PEP 420, accepted for Python 3.3, defined the namespace package: a package split across multiple directories, with portions that may reside in different locations on the file system, in zip files, or anywhere else the import machinery searches<sup>[5](https://peps.python.org/pep-0420/)</sup><sup> • </sup><sup>[14](https://docs.python.org/3.12/reference/import.html)</sup>. A namespace package has no `__init__.py`, no `__file__` attribute, and a `__path__` that is automatically recomputed when the parent path changes<sup>[5](https://peps.python.org/pep-0420/)</sup>. Module conflicts remain a practical hazard: when different packages provide modules under the same namespace, the Python ecosystem experiences module conflicts that tools have been built to detect<sup>[15](https://arxiv.org/abs/2401.02090)</sup>.

**Rust.** Rust's reference defines a namespace as a logical grouping of declared names, segregated by the kind of entity the name refers to, so that a name in one namespace does not conflict with the same name in another; the type namespace holds modules, structs, enums, traits, and type aliases, among others<sup>[6](https://doc.rust-lang.org/reference/names/namespaces.html)</sup>. Modules themselves are defined in the type namespace of their enclosing module or block, and defining multiple items with the same name in the same namespace within a module is an error<sup>[16](https://doc.rust-lang.org/stable/reference/items/modules.html)</sup>.

## Name resolution and security

Since a namespace translates the names that clients request into the underlying resources they actually obtain, this layer of indirection opens up attack routes within name resolution: attackers can deceive programs into reaching resources other than those intended, either by altering how names are bound to resources or by employing names whose destination resources are unclear.<sup>[7](https://pure.psu.edu/en/publications/a-rose-by-any-other-name-or-an-insane-root-adventures-in-name-res/)</sup> Research on this problem examined attacks across various namespaces, derived invariants to defend against them, and found that four prior enforcement techniques were each incomplete but effective in combination; a prototype enforcing per-system-call invariants on the Linux file system namespace ran with less than 3% overhead, indicating that fine-grained name resolution enforcement is practical<sup>[7](https://pure.psu.edu/en/publications/a-rose-by-any-other-name-or-an-insane-root-adventures-in-name-res/)</sup>.

Namespaces also serve isolation directly. In the Java Virtual Machine and the [Common Language Infrastructure](https://www.edgepedia.org/common-language-infrastructure), name visibility control over a local namespace in which untrusted code resides is a direct implementation of isolating software extensions, requiring a policy language for custom scoping rules and a protection mechanism to impose them<sup>[17](https://www.sciencedirect.com/science/article/abs/pii/S147784240900044X)</sup>.

## Theory and tooling

Name resolution has been formalized independently of any one language. The scope graph framework formulates resolution as a two-stage problem: a language-specific mapping from the abstract syntax tree builds a language-independent scope graph, and a language-independent resolution process then resolves references to declarations, with a resolution calculus proved sound and complete for the algorithm<sup>[10](https://web.cecs.pdx.edu/~apt/esop15.pdf)</sup>. Stack graphs, an extension of that framework, power precise code navigation at GitHub, where millions of repositories in hundreds of languages require file-incremental graph construction and a path-finding algorithm that maintains a stack of pending lookups to handle type-directed name resolution<sup>[18](https://arxiv.org/abs/2211.01224)</sup>.

## Current developments

C still lacks a language-level namespace mechanism, and prefixing identifiers remains the de facto way C developers avoid name collisions in their APIs. A proposal for the [C2y](https://www.edgepedia.org/c29-c-standard-revision) standard, WG14 document N3794 titled "Namespacing with prefixes", aims to add proper namespaces to C without implicit name mangling by utilizing prefixes<sup>[8](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3794.pdf)</sup>. A related proposal, N3491 "Namespaces Without Name Mangling", has been discussed in the WG14 document log<sup>[19](https://open-std.org/jtc1/sc22/wg14/www/wg14_document_log_v2.htm)</sup>. The debate echoes the one that preceded C++ namespaces in 1994, when prefixing was likewise the accepted workaround<sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1408.html)</sup>.

## References

1. [Namespace, MDN Web Docs Glossary](https://developer.mozilla.org/en-US/docs/Glossary/Namespace)
2. [Bjarne Stroustrup, A History of C++: 1979−1991 (HOPL-2)](https://stroustrup.com/hopl2.pdf)
3. [David Abrahams, Qualified Namespaces: Proposal for new Namespaces and Lookup Rules, WG21 N1408 (2002)](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1408.html)
4. [What Is a Package?, The Java Tutorials, Oracle](https://docs.oracle.com/javase/tutorial/java/concepts/package.html)
5. [PEP 420 – Implicit Namespace Packages](https://peps.python.org/pep-0420/)
6. [Namespaces, The Rust Reference](https://doc.rust-lang.org/reference/names/namespaces.html)
7. [Vijayakumar, Schiffman, and Jaeger, A rose by any other name or an insane root? Adventures in name resolution, EC2ND 2011](https://pure.psu.edu/en/publications/a-rose-by-any-other-name-or-an-insane-root-adventures-in-name-res/)
8. [Abdulmalek Almkainzi, Namespacing with prefixes, WG14 N3794](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3794.pdf)
9. [9. Classes, Python 3 documentation](https://docs.python.org/3/tutorial/classes.html)
10. [Néron, Tolmach, Visser, and Wachsmuth, A Theory of Name Resolution, ESOP 2015](https://web.cecs.pdx.edu/~apt/esop15.pdf)
11. [C++ International Standard working draft N4778](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4778.pdf)
12. [Packages: Commentary, CS 261, Old Dominion University](https://www.cs.odu.edu/~zeil/cs261/latest/Public/packages/index.html)
13. [Names, Java Language Specification (Java SE 6)](https://docs.oracle.com/javase/specs/jls/se6/html/names.html)
14. [5. The import system, Python 3 documentation](https://docs.python.org/3.12/reference/import.html)
15. [ModuleGuard: Understanding and Detecting Module Conflicts in Python Ecosystem, arXiv](https://arxiv.org/abs/2401.02090)
16. [Modules, The Rust Reference](https://doc.rust-lang.org/stable/reference/items/modules.html)
17. [Isolating untrusted software extensions by custom scoping rules, ScienceDirect](https://www.sciencedirect.com/science/article/abs/pii/S147784240900044X)
18. [Creager and van Antwerpen, Stack graphs: Name resolution at scale, arXiv](https://arxiv.org/abs/2211.01224)
19. [WG14 Document log](https://open-std.org/jtc1/sc22/wg14/www/wg14_document_log_v2.htm)

---
*Topic: Encyclopedia › Technology and the built world › Computing and digital systems › Software and programming › Programming languages › Programming language concepts*

*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

*Copyright 2026 EdgeChat AI, a subsidiary of Biostate AI.*

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license
