How do I create a namespace package in Python?

TL;DR: On Python 3.3 you don’t have to do anything, just don’t put any __init__.py in your namespace package directories and it will just work. On pre-3.3, choose the pkgutil.extend_path() solution over the pkg_resources.declare_namespace() one, because it’s future-proof and already compatible with implicit namespace packages. Python 3.3 introduces implicit namespace packages, see PEP 420. This … Read more

Getting all types in a namespace via reflection

Following code prints names of classes in specified namespace defined in current assembly. As other guys pointed out, a namespace can be scattered between different modules, so you need to get a list of assemblies first. string nspace = “…”; var q = from t in Assembly.GetExecutingAssembly().GetTypes() where t.IsClass && t.Namespace == nspace select t; … Read more

Namespace + functions versus static methods on a class

By default, use namespaced functions. Classes are to build objects, not to replace namespaces. In Object Oriented code Scott Meyers wrote a whole Item for his Effective C++ book on this topic, “Prefer non-member non-friend functions to member functions”. I found an online reference to this principle in an article from Herb Sutter: http://www.gotw.ca/gotw/084.htm The … Read more

What is the meaning of prepended double colon “::”?

This ensures that resolution occurs from the global namespace, instead of starting at the namespace you’re currently in. For instance, if you had two different classes called Configuration as such: class Configuration; // class 1, in global namespace namespace MyApp { class Configuration; // class 2, different from class 1 function blah() { // resolves … Read more

Using std Namespace

Most C++ users are quite happy reading std::string, std::vector, etc. In fact, seeing a raw vector makes me wonder if this is the std::vector or a different user-defined vector. I am always against using using namespace std;. It imports all sorts of names into the global namespace and can cause all sorts of non-obvious ambiguities. … Read more

Unnamed/anonymous namespaces vs. static functions

The C++ Standard reads in section 7.3.1.1 Unnamed namespaces, paragraph 2: The use of the static keyword is deprecated when declaring objects in a namespace scope, the unnamed-namespace provides a superior alternative. Static only applies to names of objects, functions, and anonymous unions, not to type declarations. Edit: The decision to deprecate this use of … Read more