Namespace (computer science)

Namespace (computer science)

A namespace (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.[1]

For example, Bill works for company X and his employee ID is 123. Jane works for company Y and her employee ID is also 123. The reason Bill and Jane can be identified by the same ID number is because they work for different companies. The different companies in this case would symbolize different namespaces. There would be serious confusion if the two people worked for the same company, and still had the same employee ID. For instance, a paycheck issued to employee ID 123 would not identify which person should receive the check.

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 (eg. 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


In C++, a namespace is defined with a namespace block.[2]

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.


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.


In Python, namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then name spaces are hierarchical too.[3][4] 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
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
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 "from ... import ... as ...." as a way of providing an alias or alternative name for use by the calling module.

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.


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 & alise the namespace
use phpstar\fooBar as FB;
$obj_foobar = new FB();
# Access the properties and methods with regular way

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:


This 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


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Scope (computer science) — In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects… …   Wikipedia

  • Reflection (computer science) — In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming .In most modern computer… …   Wikipedia

  • Class (computer science) — In object oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.More technically, a class is a cohesive… …   Wikipedia

  • Namespace — This article is about namespaces in general. For their use in computing, see Namespace (computer science). For their use in Wikipedia, see Wikipedia:Namespace. In general, a namespace is a container that provides context for the identifiers… …   Wikipedia

  • Computer file — This article is about computer files and file systems in general terms. For a more detailed and technical article, see File system. A computer file is a block of arbitrary information, or resource for storing information, which is available to a… …   Wikipedia

  • Naming convention (programming) — In computer programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types and functions etc. in source code and documentation. Reasons for using a naming convention …   Wikipedia

  • Naming conventions (programming) — In computer programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers in source code and documentation. Reasons for using a naming convention (as opposed to allowing programmers to choose… …   Wikipedia

  • NS — as an abbreviation can mean: In geography: Negeri Sembilan, one of the fourteen states in Malaysia Novi Sad, a city in Serbia Nova Scotia, as the official Canadian postal abbreviation for the province In government and politics: Nasjonal Samling …   Wikipedia

  • Common Lisp — Paradigm(s) Multi paradigm: procedural, functional, object oriented, meta, reflective, generic Appeared in 1984, 1994 for ANSI Common Lisp Developer ANSI X3J13 committee Typing discipline …   Wikipedia

  • Identifier — Identifiers on the back of a statue in the Louvre An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique class of objects, where the object or class may be an idea, physical [countable] object …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”