Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Python namespaces all about

I have just started learning Python & have come across "namespaces" concept in Python. While I got the jist of what it is, but am unable to appreciate the gravity of this concept.

Some browsing on the net revealed that one of the reasons going against PHP is that it has no native support for namespaces.

Could someone explain how to use namespaces & how this feature makes programming better (not just in Python, as I assume namespaces in not a concept limited to a particular language).

I am predominantly coming from Java and C programming backgrounds.

like image 338
Srikar Appalaraju Avatar asked Oct 12 '10 09:10

Srikar Appalaraju


People also ask

What is the main purpose of the namespace Python?

A namespace is basically a system to make sure that all the names in a program are unique and can be used without any conflict. You might already know that everything in Python—like strings, lists, functions, etc. —is an object. Another interesting fact is that Python implements namespaces as dictionaries.

What are the three types of namespaces in Python?

There are three types of Python namespaces- global, local, and built-in. It's the same with a variable scope in python. Also, the 'global' keyword lets us refer to a name in a global scope.

What is namespace and why it is used?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is difference between namespace and module in Python?

In Python-speak, modules are a namespace—a place where names are created. And names that live in a module are called its attributes. Technically, modules correspond to files, and Python creates a module object to contain all the names defined in the file; but in simple terms, modules are just namespaces.


2 Answers

Namespace is a way to implement scope.

In Java (or C) the compiler determines where a variable is visible through static scope analysis.

  • In C, scope is either the body of a function or it's global or it's external. The compiler reasons this out for you and resolves each variable name based on scope rules. External names are resolved by the linker after all the modules are compiled.

  • In Java, scope is the body of a method function, or all the methods of a class. Some class names have a module-level scope, also. Again, the compiler figures this out at compile time and resolves each name based on the scope rules.

In Python, each package, module, class, function and method function owns a "namespace" in which variable names are resolved. Plus there's a global namespace that's used if the name isn't in the local namespace.

Each variable name is checked in the local namespace (the body of the function, the module, etc.), and then checked in the global namespace.

Variables are generally created only in a local namespace. The global and nonlocal statements can create variables in other than the local namespace.

When a function, method function, module or package is evaluated (that is, starts execution) a namespace is created. Think of it as an "evaluation context". When a function or method function, etc., finishes execution, the namespace is dropped. The variables are dropped. The objects may be dropped, also.

like image 186
S.Lott Avatar answered Oct 05 '22 05:10

S.Lott


Namespaces prevent conflicts between classes, methods and objects with the same name that might have been written by different people.

Coming from a Java background you are probably familiar with how this is achieved using packages e.g. you might create a movieyoda.DateUtils class and I can create a mikej.DateUtils class and the package allows code using the classes to distinguish between them. (Python has something very similar.)

Namespaces were added to PHP in 5.3.0 but in earlier versions (and in other languages that don't provide namespaces) you would have to prefix your class and method names with something to reduce the risk of a name clash. e.g. a movieyoda_parse_file function.

like image 31
mikej Avatar answered Oct 05 '22 05:10

mikej