Questions tagged [namespaces]

A namespace is a container that provides context for identifiers, within which names are unique.

A namespace is a container that provides context for identifiers, within which names are unique. In many implementations, identifiers can be disambiguated between namespaces by prepending the identifier with the namespace, separated by a delimiter such as a period (.) in and , double-colon (::) in or backslash (\) in .

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.

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.

PHP

Namespaces were introduced into PHP from version 5.3 onwards. In PHP, a namespace is defined with a namespace block.

namespace phpstar {
    class fooBar {
        public function foo() {
            echo 'hello world, from function foo';
        }

        public function bar() {
            echo 'hello world, from function bar';
        }
    }
}

XML

In XML, the XML namespace specification enables the names of elements and attributes in an XML document to be unique. Using XML namespaces, XML documents may contain element or attribute names from more than one XML vocabulary.

Python

In Python, namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then name spaces are hierarchical as well. 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.

.NET

All .NET Framework classes are organized in namespaces. When referencing a class, one should specify either its fully qualified name, which means namespace followed by the class name,

C++

In C++, a namespace is defined with a namespace block.

namespace abc {
    int bar;
}
12341 questions
6
votes
2 answers

Use of "using" in header files

I understood, that I should not use this in a header file: using namespace foo; Because it brings the namespace foo in the global scope for anyone who uses my header file. Can I prevent this from happening, if I do it in my own namespace? For…
DyingSoul
  • 231
  • 5
  • 11
6
votes
3 answers

How can I change the application name(space) in Laravel 6

In previous versions you could use the artisan command php artisan app:name NewName to use change the applications namespace (defaults to App). The artisan command seems to be missing now. Is changing the namespace no longer supported?
kaan_a
  • 3,503
  • 1
  • 28
  • 52
6
votes
7 answers

Hiding a C++ class in a header without using the unnamed namespace

I am writing a C++ header in which I define a class A { // ... }; that I would like to hide from the outside world (because it may change or even be removed in future versions of this header). There is also a class B in the same header that has…
Bjoern
  • 63
  • 1
  • 3
6
votes
2 answers

How Python's closure works

I am trying to understand how closure works in Python, and I've found the following code snippet: def closure(): count = 0 def inner(): nonlocal count count += 1 print(count) return inner start =…
torez233
  • 193
  • 8
6
votes
5 answers

Is pimpl compatible with anonymous namespaces?

I am trying to use the pimpl pattern and define the implementation class in an anonymous namespace. Is this possible in C++? My failed attempt is described below. Is it possible to fix this without moving the implementation into a namespace with a…
anatolyg
  • 26,506
  • 9
  • 60
  • 134
6
votes
1 answer

How to set a breakpoint in gdb for an anonymous namespace?

In my code base, there are some callbacks functions which are defined in anonymous namespace. I am debugging in gdb and I want to set breakpoint in the function using function name. I also tried putting breakpoint by using filename : linenum , but…
Dipanjan
  • 181
  • 3
  • 12
6
votes
2 answers

NameSpace Issue in JointJS version 3

I am trying to convert a legacy app from JointJS v2.2.1 to v3.0.2. I’m hitting an error others have found: Uncaught Error: dia.ElementView: markup required. (joint.min.js:8) A helpful person said: “Please note you need to be careful with…
Tachyon80
  • 147
  • 10
6
votes
1 answer

How to restrict the import of a Module in F# to a local scope?

Is it possible to locally restrict the import of a module, preferrably combining this with Module Abbreviations? The goal is to avoid polluting my current module with symbols from imports. e.g. (inspired by OCaml) something like that: let…
Alexander Battisti
  • 2,178
  • 2
  • 19
  • 24
6
votes
1 answer

How to create nested namespace packages for setuptools distribution

I'm developing a python project that will have separately distributable parts. I have been able to accomplish part of my goal by making a namespace package. I have "sub1" and "sub2", both in namespace "lvl1". I can pip install these in development…
master_gibber
  • 223
  • 1
  • 8
6
votes
3 answers

Empty Function use in Javascript

I am trying to understand a third party Javascript code. But am not able to figure out what is the use of the below coding style. function A(){ } A.Prop = '23'; A.generate = function(n){ // do something } And then it is just used as…
duskandawn
  • 646
  • 1
  • 10
  • 19
6
votes
3 answers

Can't Access Kubernetes Service Exposed via NodePort

I'm using minikube to test kubernetes on latest MacOS. Here are my relevant YAMLs: namespace.yml apiVersion: v1 kind: Namespace metadata: name: micro labels: name: micro deployment.yml apiVersion: extensions/v1beta1 kind:…
XXXXX
  • 63
  • 1
  • 4
6
votes
0 answers

What is the PHPDoc package tag used for?

I created a PHP class in PhpStorm and allowed the IDE to auto-generate the DocBlock for the class. It included a package tag that exactly matched the namespace of the file as follows:
Schparky
  • 429
  • 4
  • 10
6
votes
1 answer

How do I handle namespace packages that will serve as dependencies for another package

I have written three Python modules, two of them are independent but the last one would depend on the two independent modules. For example, consider the following structure myProject/ subpackage_a/ __init__.py ... …
tryingtosolve
  • 763
  • 5
  • 20
6
votes
2 answers

Name spacing not working in vuex maps throwing module namespace not found

I'm developing a small project and i want to be able to use namespaces to avoid getters, mutations, actions, with same names. As described in docs, the modules must be imported to store, and maps must receive the path to the right module. I can…
Joel Rodrigues
  • 87
  • 1
  • 2
  • 9
6
votes
2 answers

Using whitespace in class names in Python

The normal way of creating a class in python does not allow for whitespace in the class name. >>> class Foo Bar(object): SyntaxError: invalid syntax However, using the built-in type() function, we can create a class with whitespace in the…
RasmusN
  • 147
  • 1
  • 12