不能真正比较Python和C ++中的命名空间。例如,在C ++中-
// a.h namespace ns { struct A { .. }; struct B { .. }; }
如果我们要这样做-
#include "a.h" using ns::A;
该代码的重点是能够写出不合格的A(即不必写ns::A)。现在,您可以考虑将python等效为-
from a import A
但是不管使用什么,整个ah头仍然会被包含和编译,因此我们仍然可以编写ns::B,而在Python版本中,aB将不可见。其他C ++版本,
using namespace ns;
也没有Python类似物。它在整个代码库中从命名空间ns引入所有名称-并且可以重用命名空间。例如,
#include <vector> #include <map> #include <algorithm> using namespace std; // bring in EVERYTHING
那一条线相当于-
from vector import * from map import * from algorithm import *
至少在它的作用方面,但随后它实际上只能带来命名空间std中的内容-不一定是所有内容。