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
66
votes
9 answers

What does "Symbol not found / Expected in: flat namespace" actually mean?

When I import a module I built, I get this boost-python related error: Traceback (most recent call last): File "", line 1, in ImportError: dlopen(./myMod.so, 2): Symbol not found:…
kilojoules
  • 9,768
  • 18
  • 77
  • 149
66
votes
3 answers

What namespace will a class have if no namespace is defined

In C#, if I create a class with no namespace, what namespace will I use when trying to instantiate the class? For example, assume main is... namespace NamespaceTests { class Program { static void Main(string[] args) { …
barrypicker
  • 9,740
  • 11
  • 65
  • 79
66
votes
11 answers

How do you find the namespace/module name programmatically in Ruby on Rails?

How do I find the name of the namespace or module 'Foo' in the filter below? class ApplicationController < ActionController::Base def get_module_name @module_name = ??? end end class Foo::BarController < ApplicationController …
Steropes
  • 4,600
  • 2
  • 22
  • 26
65
votes
8 answers

System.drawing namespace not found under console application

I selected console application as my C# project. But the imports that seemed to work under Windows Form project don't seem to work here. It says that the drawing namespace does not exist. using System.Drawing; using System.Drawing.Imaging; My…
klijo
  • 15,761
  • 8
  • 34
  • 49
65
votes
7 answers

Python package name conventions

Is there a package naming convention for Python like Java's com.company.actualpackage? Most of the time I see simple, potentially colliding package names like "web". If there is no such convention, is there a reason for it? What do you think of…
deamon
  • 89,107
  • 111
  • 320
  • 448
63
votes
2 answers

What does `__import__('pkg_resources').declare_namespace(__name__)` do?

In some __init__.py files of modules I saw such single line: __import__('pkg_resources').declare_namespace(__name__) What does it do and why people use it? Suppose it's related to dynamic importing and creating namespace at runtime.
rsk
  • 1,266
  • 1
  • 13
  • 20
63
votes
2 answers

How to ignore namespace when selecting XML nodes with XPath

I have to parse an XML document that looks like this:
lukegf
  • 2,147
  • 3
  • 26
  • 39
63
votes
11 answers

Namespaces in C

Is there a way to (ab)use the C preprocessor to emulate namespaces in C? I'm thinking something along these lines: #define NAMESPACE name_of_ns some_function() { some_other_function(); } This would get translated to: name_of_ns_some_function()…
Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
63
votes
7 answers

Should I have a separate assembly for interfaces?

We currently have quite a few classes in a project, and each of those classes implement an interface, mostly for DI reasons. Now, my personal feeling is that these interfaces should be put into a separate namespace within the same assembly (so we…
David_001
  • 5,703
  • 4
  • 29
  • 55
63
votes
4 answers

static vs non-static variables in namespace

I have a namespace foo which contains an integer bar, declared so... foo.h: namespace foo { int bar; } Now if I include foo.h in only one file, this works just fine. But a problem arises when I include foo.h from two or more files: I get a…
Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
62
votes
1 answer

Namespace without a name in C++

Possible Duplicate: Unnamed/anonymous namespaces vs. static functions I came across this code namespace ABC { namespace DEF { namespace { I expected the namespace should be followed by some name, but it's not the case with this code. Is this…
prosseek
  • 182,215
  • 215
  • 566
  • 871
62
votes
4 answers

How to create a namespace if it doesn't exists from HELM templates?

I have a kind: Namespace template YAML, as per below: apiVersion: v1 kind: Namespace metadata: name: {{ .Values.namespace }} namespace: "" How do I make helm install create the above-given namespace ({{ .Values.namespace }}) if and only if…
62
votes
5 answers

Uses for anonymous namespaces in header files

Someone asserted on SO today that you should never use anonymous namespaces in header files. Normally this is correct, but I seem to remember once someone told me that one of the standard libraries uses anonymous namespaces in header files to…
David Norman
  • 19,396
  • 12
  • 64
  • 54
62
votes
2 answers

What is the use of "using namespace std"?

What is the use of using namespace std? I'd like to see explanation in Layman terms.
Jarvis
  • 629
  • 1
  • 6
  • 4
62
votes
5 answers

string in namespace std does not name a type

This may just be a simple mistake that I'm not seeing, but I think I'm simply doing something wrong. Don't worry I'm not using namespace std in my header functions or anything which seemed to be this person's issue [Question I read similar to…
user1581100