In computing, a namespace is a set of signs ( names) that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily Identifier.
Namespaces are commonly structured as hierarchies to allow reuse of names in different contexts. As an analogy, consider a system of Anthroponymy where each person has a given name, as well as a family name shared with their relatives. If the first names of family members are unique only within each family, then each person can be uniquely identified by the combination of first name and family name; there is only one Jane Doe, though there may be many Janes. Within the namespace of the Doe family, just "Jane" suffices to unambiguously designate this person, while within the "global" namespace of all people, the full name must be used.
Prominent examples for namespaces include , which assign names to files. Some programming languages organize their variables and in namespaces. and distributed systems assign names to resources, such as , printers, , and remote files. can partition kernel resources by isolated namespaces to support virtualization containers.
Similarly, hierarchical file systems organize files in directories. Each directory is a separate namespace, so that the directories "letters" and "invoices" may both contain a file "to_jane".
In computer programming, namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality and to avoid between multiple identifiers that share the same name.
In Computer network, the Domain Name System organizes websites (and other resources) into hierarchical namespaces.
This XML carries HTML table information:
Apples
Oranges
If these XML fragments were added together, there would be a name conflict. Both contain a element, but the elements have different content and meaning.
An XML parser will not know how to handle these differences.
The following XML distinguishes between information about the HTML table and furniture by prefixing "h" and "f" at the beginning of the elements.
In augmented Backus–Naur form:
+ Examples of names in a namespace | |
readme.txt (file name) | |
example.com (domain name) | |
array (struct) | |
NYC (locality) | |
body (element) | |
errstr (variable) | |
Date (class) | |
fi-fe19991055 | |
1000/182 (handle local name) | |
182 (publication) | |
67-89-ab (NIC specific) | |
abcd (device ID) | |
2341 003f Stephen J. Gowdy. "List of USB ID's". 2013. | 003f (product ID) |
Sydney |
A hierarchy is recursive if the syntax for the namespace names is the same for each subdelegation. An example of a recursive hierarchy is the Domain name.
An example of a non-recursive hierarchy are Uniform Resource Name representing an Internet Assigned Numbers Authority (IANA) number.
+ Hierarchical namespace breakdown for urn:isbn:978-3-16-148410-0, an identifier for the book The Logic of Scientific Discovery by Karl Popper, 10th edition. |
Formal URN namespace |
International Standard Book Numbers as Uniform Resource Names |
Bookland |
German-speaking countries |
Mohr Siebeck |
+Examples of naming systems with local and global scope, and with and without namespaces ! !! Without a namespace !! With a namespace |
Filesystem Hierarchy Standard |
Domain Name System |
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:
As well as its abstract language technical usage as described above, some languages have a specific keyword used for explicit namespace control, amongst other uses. Below is an example of a namespace in C++:
// This is how one brings a name into the current scope. In this case, it's
// bringing them into global scope.
using std::cout;
using std::endl;
namespace box1 {
namespace box2 {
int main() {
int box_side = 4;
}
int box_side = 12;
}
int box_side = 42;
cout << box1::box_side << endl; // Outputs 4.
cout << box2::box_side << endl; // Outputs 12.
cout << box_side << endl; // Outputs 42.
}
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 or documents it is common 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.
return a + b;}
const struct {
double pi; int (*add) (int, int);} helper = { 3.14, _add };
// helper.h const struct {
double pi; int (*add) (int, int);} helper;
// main.c
int main(){
printf("3 + 2 = %d\n", helper.add(3, 2)); printf("pi is %f\n", helper.pi);}
int bar;}
Identifiers that are not explicitly declared within a namespace are 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 . Although namespaces are used extensively in recent C++ code, most older code does not use this facility because it did not exist in early versions of the language. For example, the entire C++ Standard Library is defined within namespace std, but before standardization many components were originally in the global namespace. A programmer can insert the using directive to bypass namespace resolution requirements and obtain backwards compatibility with older code that expects all identifiers to be in the global namespace. However the use of the using directive for reasons other than backwards compatibility (e.g., convenience) is considered to be against good code practices.
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 contains classes core to the language, and 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.
or add a using statement. This eliminates the need to mention the complete name of all classes in that namespace.
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.
Modulea.func1() Modulea.func2() a = Modulea.Class1()
The from ... import ... statement 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:
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()
from Modulea import func1
A special form of the statement is from ... import * which imports all names defined in the named package directly in the calling module'
"in general the practice of importing * from a module or package is frowned upon"
Python also supports import x as y as a way of providing an alias or alternative name for use by the calling module:
a = np.arange(1000)
namespace phpstar;
class FooBar {
public function foo(): void { echo 'Hello world, from function foo'; }
public function bar(): void { echo 'Hello world, from function bar'; }}
png_create_write_struct png_get_signature png_read_row png_set_invalid
This naming convention provides reasonable assurance that the are unique and can therefore be used in larger programs without . Likewise, many packages originally written in Fortran (e.g., BLAS, LAPACK) reserve the first few letters of a function's name to indicate the group to which the function belongs.
This technique has several drawbacks:
It also has a few advantages:
|
|