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
279
votes
12 answers

What are namespaces?

What are PHP Namespaces? What are Namespaces in general? A Layman answer with an example would be great.
Imran
  • 11,350
  • 20
  • 68
  • 78
273
votes
34 answers

Namespace "stuck" as Terminating, How I removed it

I had a "stuck" namespace that I deleted showing in this eternal "terminating" status.
ximbal
  • 3,178
  • 2
  • 17
  • 19
269
votes
11 answers

How to switch namespace in kubernetes

Say, I have two namespaces k8s-app1 and k8s-app2 I can list all pods from specific namespace using the below command kubectl get pods -n We need to append namespace to all commands to list objects from the respective namespaces. Is…
P Ekambaram
  • 15,499
  • 7
  • 34
  • 59
243
votes
16 answers

How do you properly use namespaces in C++?

I come from a Java background, where packages are used, not namespaces. I'm used to putting classes that work together to form a complete object into packages, and then reusing them later from that package. But now I'm working in C++. How do you use…
Marius
  • 57,995
  • 32
  • 132
  • 151
242
votes
4 answers

What does a \ (backslash) do in PHP (5.3+)?

What does a \ do in PHP? For example, CSRF4PHP has \FALSE, \session_id, and \Exception: public function __construct($timeout=300, $acceptGet=\FALSE){ $this->timeout = $timeout; if (\session_id()) { $this->acceptGet = (bool)…
Alfred
  • 60,935
  • 33
  • 147
  • 186
229
votes
14 answers

type object 'datetime.datetime' has no attribute 'datetime'

I have gotten the following error: type object 'datetime.datetime' has no attribute 'datetime' On the following line: date = datetime.datetime(int(year), int(month), 1) Does anybody know the reason for the error? I imported datetime with from…
Chris Frank
  • 4,124
  • 4
  • 30
  • 42
218
votes
6 answers

Django: "projects" vs "apps"

I have a fairly complex "product" I'm getting ready to build using Django. I'm going to avoid using the terms "project" and "application" in this context, because I'm not clear on their specific meaning in Django. Projects can have many apps. Apps…
Dolph
  • 49,714
  • 13
  • 63
  • 88
218
votes
20 answers

Sharing secret across namespaces

Is there a way to share secrets across namespaces in Kubernetes? My use case is: I have the same private registry for all my namespaces and I want to avoid creating the same secret for each.
matth3o
  • 3,229
  • 3
  • 20
  • 24
214
votes
3 answers

What is the difference between * and *|* in CSS?

In CSS, * will match any element. Frequently, *|* is used instead of * to match all elements. This is generally used for testing purposes. What is the difference between * and *|* in CSS?
210
votes
9 answers

printf with std::string?

My understanding is that string is a member of the std namespace, so why does the following occur? #include int main() { using namespace std; string myString = "Press ENTER to quit program!"; cout << "Come up and C++ me some…
Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162
192
votes
24 answers

How do I get an object's unqualified (short) class name?

How do I check the class of an object within the PHP name spaced environment without specifying the full namespaced class. For example suppose I had an object library/Entity/Contract/Name. The following code does not work as get_class returns the…
Greg.Forbes
  • 2,634
  • 4
  • 26
  • 29
192
votes
5 answers

In C++, what is a "namespace alias"?

What is a "namespace alias" in C++? How is it used?
Martin B
  • 23,670
  • 6
  • 53
  • 72
178
votes
13 answers

What is the best way to solve an Objective-C namespace collision?

Objective-C has no namespaces; it's much like C, everything is within one global namespace. Common practice is to prefix classes with initials, e.g. if you are working at IBM, you could prefix them with "IBM"; if you work for Microsoft, you could…
Mecki
  • 125,244
  • 33
  • 244
  • 253
178
votes
13 answers

Python ElementTree module: How to ignore the namespace of XML files to locate matching element when using the method "find", "findall"

I want to use the method of findall to locate some elements of the source xml file in the ElementTree module. However, the source xml file (test.xml) has namespaces. I truncate part of xml file as sample:
KevinLeng
  • 1,843
  • 2
  • 12
  • 7
177
votes
9 answers

Visibility of global variables in imported modules

I've run into a bit of a wall importing modules in a Python script. I'll do my best to describe the error, why I run into it, and why I'm tying this particular approach to solve my problem (which I will describe in a second): Let's suppose I have a…
Nubarke
  • 2,020
  • 2
  • 13
  • 12