Namespace
In general, a namespace is a container for a set of identifiers (also known as symbols, names).[1][2] Namespaces provide a level of indirection to specific identifiers, thus making it possible to distinguish between identifiers with the same exact name. For example, a surname could be thought of as a namespace that makes it possible to distinguish people who have the same first name. In computer programming, namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality.
Explanation for beginners
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.
This XML carries HTML table information:
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
This XML carries information about a table (i.e. a piece of furniture):
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> <br>
If these XML fragments were added together, there would be a name conflict. Both contain a <table> element, but the elements have different content and meaning.
An XML parser will not know how to handle these differences.
Solving the Name Conflict Using a Prefix
Name conflicts in XML can easily be avoided using a name prefix.
This XML carries information about an HTML table, and a piece of furniture:
<h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table>
Naming system
A name in a namespace consists of a namespace identifier and a local name.[4][5] The namespace name is usually applied as a prefix to the local name.
In Augmented Backus-Naur Form:
name = <namespace identifier> separator <local name>
When local names are used by themselves, name resolution is used to decide which (if any) particular item is alluded to by some particular local name.
Examples
Context | Identifier | Namespace identifier | Local name |
---|---|---|---|
Path | /home/user/readme.txt | /home/user (path) | readme.txt (file name) |
Domain name | www.example.com | example.com (domain) | www (host name) |
C++ | std::array | std | array |
UN/LOCODE | US NYC | US (country) | NYC (locality) |
XML | xmlns:xhtml="http://www.w3.org/1999/xhtml" <xhtml:body> | http://www.w3.org/1999/xhtml | body |
Perl | $DBI::errstr | DBI | $errstr |
Java | java.util.Date | java.util | Date |
Uniform resource name (URN) | urn:nbn:fi-fe19991055 | urn:nbn (National Bibliography Numbers) | fi-fe19991055 |
Handle System | 10.1000/182 | 10 (Handle naming authority) | 1000/182 (Handle local name) |
Digital object identifier | 10.1000/182 | 10.1000 (publisher) | 182 (publication) |
MAC address | 01-23-45-67-89-ab | 01-23-45 (organizationally unique identifier) | 67-89-ab (NIC specific) |
PCI ID | 1234 abcd | 1234 (Vendor ID) | abcd (Device ID) |
USB VID/PID | 2341 003f[6] | 2341 (vendor ID) | 003f (product ID) |
Delegation
Namespaces allow delegation of identifier assignment to multiple name issuing organisations whilst retaining global uniqueness.[7] A central Registration authority registers the assigned namespace identifiers allocated. Each namespace identifier is allocated to an organisation which is subsequently responsible for the assignment of names in their allocated namespace. This organisation may be a name issuing organisation that assign the names themselves, or another Registration authority which further delegates parts of their namespace to different organisations.
Hierarchy
A naming scheme that allows subdelegation of namespaces to third parties is a hierarchical namespace
A hierarchy is recursive if the syntax for the namespace identifiers is the same for each subdelegation. An example of a recursive hierarchy is the Domain name system.
An example of a non-recursive hierarchy are Uniform resource name representing an ISBN 13 number.
Registry | Registrar | Example Identifier | Namespace identifier | Namespace |
---|---|---|---|---|
Uniform resource name (URN) | Internet Assigned Numbers Authority | urn:isbn:978-3-16-148410-0 | urn | Formal URN namespace |
Formal URN namespace | Internet Assigned Numbers Authority | urn:isbn:978-3-16-148410-0 | isbn | International Standard Book Numbers as Uniform Resource Names |
International Article Number (EAN) | GS1 | 978-3-16-148410-0 | 978 | Bookland |
International Standard Book Number (ISBN) | International ISBN Agency | 3-16-148410-X | 3 | German-speaking countries |
German publisher code | Agentur für Buchmarktstandards | 3-16-148410-X | 16 | Mohr Siebeck |
Namespace versus scope
A namespace identifier may provide context (Scope in computer science) to a name, and the terms are sometimes used interchangeably. However, the context of a name may also be provided by other factors, such as the location where it occurs or the syntax of the name.
Without a namespace | With a namespace | |
---|---|---|
Local scope | Vehicle registration plate | Relative path in a File system |
Global scope | Universally unique identifier | Domain Name System |
In programming languages
For many programming languages, namespace is a context for their identifiers. In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory, but one file may have the same name multiple times.[8]
As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace. A namespace is also called a context, because the same name in different namespaces can have different meanings, each one appropriate for its namespace.
Following are other characteristics of namespaces:
- Names in the namespace can represent objects as well as concepts, be the namespace a natural or ethnic language, a constructed language, the technical terminology of a profession, a dialect, a sociolect, or an artificial language (e.g., a programming language).
- In the Java programming language, identifiers that appear in namespaces have a short (local) name and a unique long "qualified" name for use outside the namespace.
- Some compilers (for languages such as C++) combine namespaces and names for internal use in the compiler in a process called name mangling.
Below is an example of a namespace in C++:
namespace Box1{ int boxSide = 4; } namespace Box2{ int boxSide = 12; } int main () { int boxSide = 42; cout << Box1::boxSide << endl; //output 4 cout << Box2::boxSide << endl; //output 12 cout << boxSide << endl; // output 42 return 0; }
Computer science considerations
A namespace in computer science (sometimes also called a name scope), is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its definition) belongs.[9]
This concept can be illustrated with an analogy. Imagine that two companies, X and Y, each assign ID numbers to their employees. X should not have two employees with the same ID number, and likewise for Y; but it is not a problem for the same ID number to be used at both companies. For example, if Bill works for company X and Jane works for company Y, then it is not a problem for each of them to be employee #123. In this analogy, the ID number is the identifier, and the company serves as the namespace. It does not cause problems for the same identifier to identify a different person in each namespace.
In large computer programs or documents it is not uncommon to have hundreds or thousands of identifiers. Namespaces (or a similar technique, see Emulating namespaces) provide a mechanism for hiding local identifiers. They provide a means of grouping logically related identifiers into corresponding namespaces, thereby making the system more modular.
Data storage devices and many modern programming languages support namespaces. Storage devices use directories (or folders) as namespaces. This allows two files with the same name to be stored on the device so long as they are stored in different directories. In some programming languages (e.g. C++, Python), the identifiers naming namespaces are themselves associated with an enclosing namespace. Thus, in these languages namespaces can nest, forming a namespace tree. At the root of this tree is the unnamed global namespace.
Use in common languages
- C++
In C++, a namespace is defined with a namespace block.[10]
namespace abc { int bar; }
Within this block, identifiers can be used exactly as they are declared. Outside of this block, the namespace specifier must be prefixed. For example, outside of namespace abc
, bar
must be written abc::bar
to be accessed. C++ includes another construct that makes this verbosity unnecessary. By adding the line
using namespace abc;
to a piece of code, the prefix abc::
is no longer needed.
Code that is not explicitly declared within a namespace is considered to be in the global namespace.
Namespace resolution in C++ is hierarchical. This means that within the hypothetical namespace food::soup
, the identifier chicken
refers to food::soup::chicken
. If food::soup::chicken
doesn't exist, it then refers to food::chicken
. If neither food::soup::chicken
nor food::chicken
exist, chicken
refers to ::chicken
, an identifier in the global namespace.
Namespaces in C++ are most often used to avoid naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this facility. For example, the entire C++ standard library is defined within namespace std
, but before standardization many components were originally in the global namespace.
- Java
In Java, the idea of a namespace is embodied in Java packages. All code belongs to a package, although that package need not be explicitly named. Code from other packages is accessed by prefixing the package name before the appropriate identifier, for example class String
in package java.lang
can be referred to as java.lang.String
(this is known as the fully qualified class name). Like C++, Java offers a construct that makes it unnecessary to type the package name (import
). However, certain features (such as reflection) require the programmer to use the fully qualified name.
Unlike C++, namespaces in Java are not hierarchical as far as the syntax of the language is concerned. However, packages are named in a hierarchical manner. For example, all packages beginning with java
are a part of the Java platform—the package java.lang
contains classes core to the language, and java.lang.reflect
contains core classes specifically relating to reflection.
In Java (and Ada, C#, and others), namespaces/packages express semantic categories of code. For example, in C#, namespace System
contains code provided by the system (the .NET Framework). How specific these categories are and how deep the hierarchies go differ from language to language.
Function and class scopes can be viewed as implicit namespaces that are inextricably linked with visibility, accessibility, and object lifetime.
- C#
Namespaces are heavily used in C# language. All .NET Framework classes are organized in namespaces, to be used more clearly and to avoid chaos. Furthermore, custom namespaces are extensively used by programmers, both to organize their work and to avoid naming collisions. When referencing a class, one should specify either its fully qualified name, which means namespace followed by the class name,
System.Console.WriteLine("Hello World!"); int i = System.Convert.ToInt32("123");
or add a using statement. This, eliminates the need to mention the complete name of all classes in that namespace.
using System; . . . Console.WriteLine("Hello World!"); int i = Convert.ToInt32("123");
In the above examples, System is a namespace, and Console and Convert are classes defined within System.
- Python
In Python, namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then name spaces are hierarchical too.[11][12] In general when a module is imported then the names defined in the module are defined via that module's name space, and are accessed in from the calling modules by using the fully qualified name.
# assume modulea defines two functions : func1() and func2() and one class : class1 import modulea modulea.func1() modulea.func2() a = modulea.class1()
The "from ... import ..." can be used to insert the relevant names directly into the calling module's namespace, and those names can be accessed from the calling module without the qualified name :
# assume modulea defines two functions : func1() and func2() and one class : class1 from modulea import func1 func1() func2() # this will fail as an undefined name, as will the full name modulea.func2() a = class1() # this will fail as an undefined name, as will the full name modulea.class1()
Since this directly imports names (without qualification) it can overwrite existing names with no warnings.
A special form is "from ... import *", which imports all names defined in the named package directly in the calling modules namespace. Use of this form of import, although supported within the language, is generally discouraged as it pollutes the namespace of the calling module and will cause already defined names to be overwritten in the case of name clashes.
Python also supports "import x as y" as a way of providing an alias or alternative name for use by the calling module:
import numpy as np a = np.arange(1000)
- XML namespace
In XML, the XML namespace specification enables the names of elements and attributes in an XML document to be unique, similar to the role of namespaces in programming languages. Using XML namespaces, XML documents may contain element or attribute names from more than one XML vocabulary.
- PHP
Namespaces were introduced into PHP from version 5.3 onwards. Naming collision of classes, functions and variables can be avoided. In PHP, a namespace is defined with a namespace block.
# assume this is a class file defines two functions : foo() and bar() # location of the file phpstar/foobar.php namespace phpstar; class fooBar { public function foo() { echo 'hello world, from function foo'; } public function bar() { echo 'hello world, from function bar'; } }
We can reference a PHP namespace with following different ways:
# location of the file index.php # Include the file include "phpstar/foobar.php"; # Option 1: directly prefix the class name with the namespace $obj_foobar = new \phpstar\fooBar(); # Option 2: import the namespace use phpstar\fooBar; $obj_foobar = new fooBar(); # Option 2a: import & alias the namespace use phpstar\fooBar as FB; $obj_foobar = new FB(); # Access the properties and methods with regular way $obj_foobar->foo(); $obj_foobar->bar();
Emulating namespaces
In programming languages lacking language support for namespaces, namespaces can be emulated to some extent by using an identifier naming convention. For example, C libraries e.g. Libpng often use a fixed prefix for all functions and variables that are part of their exposed interface. Libpng exposes identifiers such as:
png_create_write_struct png_get_signature png_read_row png_set_invalid
This naming convention provides reasonable assurance that the identifiers are unique and can therefore be used in larger programs without fear of identifier naming collisions. Likewise, many packages originally written in Fortran (e.g., BLAS, LAPACK) reserve the first few letters of a function's name to indicate which group it belongs to.
Unfortunately, this technique has several drawbacks:
- It doesn't scale well to nested namespaces; identifiers become excessively long.
- Individuals or organizations may use dramatically inconsistent naming conventions, potentially introducing unwanted obfuscation.
- Compound or 'query-based' operations on groups of identifiers, based on the namespaces in which they are declared, is rendered unwieldy or unfeasible.
- All uses of the identifiers must, in effect, be fully namespace-qualified. Languages with direct support for namespaces usually provide ways for the programmer to declare up front that they wish to use some or all identifiers from a specific namespace, which they can then use without qualification for the remainder of the block.
- In languages in which identifier length is restricted, the use of prefixes limits the number of characters that can be used to identify what the function does. This is a particular problem for packages originally written in FORTRAN 77 that offered only 6 characters per identifier. For example, the BLAS function
DGEMM
function indicates that it operates on double precision numbers (the 'D'), general matrices (the 'GE') and only the last two characters show what it actually does: a matrix-matrix multiplication (the 'MM').
See also
Look up namespace in Wiktionary, the free dictionary. |
- 11-Digit Delivery point ZIP code
- Binomial nomenclature (genus-species in biology)
- Chemical nomenclature
- Dewey Decimal Classification
- Digital object identifier
- Domain name system (DNS) names (Internet addresses)
- Identity (object-oriented programming)
- IP address
- Library of Congress Classification
- Category:National identification numbers
- Star catalogues and astronomical naming conventions
- XML Namespaces
References
- ↑ "C# FAQ: What is a namespace". http://en.csharp-online.net/: C# Online Net. Retrieved 2010-02-23. "A namespace is nothing but a group of assemblies, classes, or types. A namespace acts as a container—like a disk folder—for classes organized into groups usually based on functionality. C# namespace syntax allows namespaces to be nested."
- ↑ "namespace". Webopedia. Retrieved 2012-03-30. "The set of names in a naming system."
- ↑ W3schools. "Name Conflicts".
- ↑ XML Core Working Group (8 December 2009). "Namespaces in XML 1.0 (Third Edition)". W3C. Retrieved 2012-03-30.
- ↑ Moats, Ryan (May 1997). "Syntax". URN Syntax. IETF. p. 1. sec. 2. RFC 2141. https://tools.ietf.org/html/rfc2141#section-2. Retrieved 2012-03-30.
- ↑ Stephen J. Gowdy. "List of USB ID's". 2013.
- ↑ Sollins & Masinter (December 1994). "Requirements for functional capabilities". Functional Requirements for Uniform Resource Names. IETF. p. 3. sec. 2. RFC 1731. https://tools.ietf.org/html/rfc1731#section-2. Retrieved 2012-03-30.
- ↑ "C# FAQ: What is a namespace". http://en.csharp-online.net/: C# Online Net. Retrieved 2010-02-23. "For instance, [under Windows], to access the built-in input-output (I/O) classes and members, use the System.IO namespace. Or, to access Web-related classes and members, use the System.Web namespace."
- ↑ "A namespace is "a logical grouping of the names used within a program."". Webopedia.com. Retrieved 2011-07-26.
- ↑ "Namespaces allow to group entities like classes, objects and functions under a name.". Cplusplus.com. Retrieved 2011-07-26.
- ↑ "6. Modules". The Python Tutorial. Python Software Foundation. Retrieved 25 October 2010.
- ↑ "Python Scopes and Namespaces". Docs.python.org. Retrieved 2011-07-26.