NamespaceIn 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 identified. Namespaces are commonly structured as hierarchies to allow reuse of names in different contexts. As an analogy, consider a system of naming of people 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 file systems, which assign names to files.[1] Some programming languages organize their variables and subroutines in namespaces.[2][3][4] Computer networks and distributed systems assign names to resources, such as computers, printers, websites, and remote files. Operating systems 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 name collisions between multiple identifiers that share the same name. In networking, the Domain Name System organizes websites (and other resources) into hierarchical namespaces. Name conflictsElement 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>Oranges</td>
</tr>
</table>
This XML carries information about a table (i.e. a piece of furniture): <table>
<name>Mahogany Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these XML fragments were added together, there would be a name conflict. Both contain a An XML parser will not know how to handle these differences. Solution via prefixName conflicts in XML can easily be avoided using a name prefix. The following XML distinguishes between information about the HTML table and furniture by prefixing "h" and "f" at the beginning of the elements. <h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Oranges</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>Mahogany Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
Naming systemA name in a namespace consists of a namespace name and a local name.[5][6] The namespace name is usually applied as a prefix to the local name. In augmented Backus–Naur form: name = <namespace name> separator <local name>
When local names are used by themselves, name resolution is used to decide which (if any) particular name is alluded to by some particular local name. Examples
DelegationDelegation of responsibilities between parties is important in real-world applications, such as the structure of the World Wide Web. Namespaces allow delegation of identifier assignment to multiple name issuing organisations whilst retaining global uniqueness.[8] A central Registration authority registers the assigned namespace names allocated. Each namespace name 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. HierarchyA naming scheme that allows subdelegation of namespaces to third parties is a hierarchical namespace. 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 system. An example of a non-recursive hierarchy are Uniform Resource Name representing an Internet Assigned Numbers Authority (IANA) number.
Namespace versus scopeA namespace name 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.
In programming languagesFor 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.[9] 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++: import std;
// This is how one brings a name into the current scope. In this case, it's
// bringing them into global scope.
using std::println;
namespace box1 {
constexpr int BOX_SIDE = 4;
}
namespace box2 {
constexpr int BOX_SIDE = 12;
}
int main() {
constexpr int BOX_SIDE = 42;
println("{}", box1::BOX_SIDE); // Outputs 4.
println("{}", box2::BOX_SIDE); // Outputs 12.
println("{}", BOX_SIDE); // Outputs 42.
}
Computer-science considerationsA 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, 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.[10] 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 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. Use in common languagesCIt is possible to use anonymous structs as namespaces in C since C99. Math.h: #pragma once
const struct {
double PI;
double (*sin)(double);
} Math;
Math.c: #include <math.h>
static double _sin(double arg) {
return sin(arg);
}
const struct {
double PI;
double (*sin)(double);
} Math = { M_PI, _sin };
Main.c: #include <stdio.h>
#include "Math.h"
int main() {
printf("sin(0) = %d\n", Math.sin(0));
printf("pi is %f\n", Math.PI);
}
C++In C++, a namespace is defined with a namespace block.[11] 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 using namespace abc;
to a piece of code, the prefix Identifiers that are not explicitly declared within a namespace are considered to be in the global namespace. int foo;
These identifiers can be used exactly as they are declared, or, since the global namespace is unnamed, the namespace specifier Namespace resolution in C++ is hierarchical. This means that within the hypothetical 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 because it did not exist in early versions of the language. For example, the entire C++ Standard Library is defined within The use of the
export module com.acme.project.App;
import std;
import com.acme.project.fs;
import com.acme.project.util;
using com::acme::project::fs::File;
using com::acme::project::util::ConfigLoader;
using com::acme::project::util::logging::Logger;
using com::acme::project::util::logging::LoggerFactory;
export namespace com::acme::project {
class App {
private:
Logger logger;
// private fields and methods
public:
App():
logger{LoggerFactory::getLogger("Main")} {
ConfigLoader cl(File("config/config_file.txt"));
logger.log("Application starting...");
// rest of code
}
};
}
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. Unlike C++, namespace Acme.Project;
using System;
using System.IO;
using Microsoft.Extensions.Logging;
using Acme.Project.Utility;
class App
{
private static ILogger<Program> logger;
public App()
{
ConfigLoader cl = new ConfigLoader(Path.Combine("config", "config_file.txt"));
LoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});
logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("Application starting...");
// rest of code
}
}
Unlike C++, C# namespaces do not allow relative referencing of symbols. For example, the class JavaIn 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 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 In Java (and Ada, C#, and others), namespaces/packages express semantic categories of code. For example, in C#, Function and class scopes can be viewed as implicit namespaces that are inextricably linked with visibility, accessibility, and object lifetime. In Java, packages cannot be partially qualified like they can in C++. For instance, it is not possible to import the package com.acme.project;
import java.nio.file.Paths;
import java.util.logging.Logger;
import java.util.logging.Level;
import com.acme.project.util.ConfigLoader;
public class App {
private static final Logger logger = Logger.getLogger(Main.class.getName());
public App() {
ConfigLoader cl = new ConfigLoader(Paths.get("config/config_file.txt"));
logger.log(Level.INFO, "Application starting...");
// rest of code
}
}
Because Java does not support independent functions outside of classes, static class methods and so-called "utility classes" (classes with private constructors and all methods and fields static) are the equivalent to C++-style namespaces. Some examples are
import java.sql.*; // Imports all classes in java.sql, including java.sql.Date
import java.util.*; // Imports all classes in java.util, including java.util.Date
Date d = new Date(); // Ambiguous Date reference resulting in compilation error
// Instead, the fully-qualified names must be used:
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
java.util.Date utilDate = new java.util.Date();
PHPNamespaces 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. # File phpstar/foobar.php
namespace phpstar;
class FooBar
{
public function foo(): void
{
echo 'Hello world, from function foo';
}
public function bar(): void
{
echo 'Hello world, from function bar';
}
}
We can reference a PHP namespace with the following different ways: # 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();
PythonIn Python, namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then namespaces are hierarchical too.[12][13] In general when a module is imported then the names defined in the module are defined via that module's namespace, 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 = Modulea.Class1()
The # 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 = 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 of the statement is from selenium.webdriver import Firefox
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
if __name__ == "__main__":
driver: Firefox = Firefox()
element: WebElement = driver.find_element(By.ID, "myInputField")
element.send_keys(f"Hello World{Keys.ENTER}")
action: ActionChains = ActionChains(driver)
action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
Python also supports import numpy as np
a: np.ndarray = np.arange(1000)
RustIn Rust, a namespace is called a "module" and declared using Similar to the mod my_module {
pub trait Greet {
fn greet(&self);
}
pub struct Person {
pub name: String,
}
impl Greet for Person {
fn greet(&self) {
println!("Hello, {}!", self.name);
}
}
}
fn main() {
use my_module::{Person, Greet};
let person = Person { name: String::from("Alice") };
person.greet();
}
Writing mod util;
use std::fs::File;
use crate::util::ConfigLoader;
use crate::util::logging::{Logger, LoggerFactory};
pub struct App {
config_loader: ConfigLoader;
}
impl App {
pub fn new() -> Self {
config_loader = ConfigLoader::new(File::open("config/config_file.txt"));
config_loader.load();
let logger: Logger = LoggerFactory::get_logger("Main");
logger.log("Application starting...");
// rest of code
}
}
The use std::{
fmt::*, // imports all symbols in std::fmt
fs::{File, Metadata}, // imports std::fs::File and std::fs::Metadata
io::{prelude::*, BufReader, BufWriter} // imports all symbols in std::io::prelude::*, std::io::BufReader, and std::io::BufWriter
process, // imports the std::process namespace (for example std::process::Command can be referred to as process::Command)
time // imports the std::time namespace
};
XML namespaceIn 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. Emulating namespacesIn programming languages lacking language support for namespaces, namespaces can be emulated to some extent by using an identifier naming convention. For example, C libraries such as 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 naming collisions.[15] 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:
See also
References
|