Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of Python namespaces

Tags:

I am looking for a good introduction to Python namespaces. I found this tutorial to be good, but I'm sure there is more to the story than that.

The Python reference contains information but it's quite hard to read.

Please explain python namespaces. In your explanation please include complete examples that show the various uses.

like image 785
ElenaT Avatar asked Feb 09 '11 21:02

ElenaT


People also ask

What is namespace used for in 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.

How many namespace are there in Python?

In Python, there are four types of namespaces which are given below. As these namespace various have lifetimes, Python interpreter creates namespaces as necessary and deletes them when they are no longer needed. Let's understand the various types of namespace in Python.

What is difference between namespace and scope in Python?

A namespace is a mapping from names to objects . A scope is a textual region of a Python program where a namespace is directly accessible.


1 Answers

They're really not all that complex. The import mechanisms can be somewhat complex and with relative imports now it can get even a bit more tricky, but as far as actual namespaces and how they're resolved in code, that tutorial seems to be pretty comprehensive.

The language reference is always kind of the be-all, end-all however, but it's often overkill for those starting out.

If you have any specific questions as to how certain things work, you'll get good responses on here.

like image 183
Daniel DiPaolo Avatar answered Sep 19 '22 16:09

Daniel DiPaolo