Python Forum
Still not completely clear about namespaces...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Still not completely clear about namespaces...
#1
Hello Forum,

a) I understand that everything that gets loaded in RAM (classes, variables, lists, modules, etc.) has a name. The object and the name of each object are recorded somewhere in RAM when the object is used. The name of each object is part of a naming structure which is like a phonebook. That phonebook is like a symbol table and is called namespace.

b) Thanks to namespaces, the Python interpreter can access variables, classes, et.c and avoid naming conflicts. Each namespace is essentially a different symbol table/name table. Functions have their own private namespace (local namespace), modules have their namespace (module namespace), the standard library has its own namespace (built-in namespace), and the running Python program has also its namespace (global namespace).

c) Each namespace has its own associated dictionary system (name:object mapping). Using the dot notation, we type <moduleName>.<moduleAttribute>. It looks like in some cases we need to specify the object's name to access its attributes but we don't in other cases if the objects' names are inside the built-in namespace or the global namespace...

d) Variables have a scope. Only variables have a scope or even other types of objects? For example, variables defined inside a function or class are only usable within and by that function or class and are local variables with a local scope. On the other hand, variables that can be used anywhere in the code are global. We run into issues when global and local variables have the same name...Creating global variables is discouraged...But how can we not create global variables if we write a Python script? We can certainly use modules and import functions, classes, etc. Do global scope variables/objects belong to a specific namespace or do they exist in all namespaces at the same time?

e) Is it correct to think that every namespace has its own scope and visibility? functions have a private, local namespace (all its internal variables cannot be seen outside of the function). But the function can see variables in the global namespace. Functions look inside their namespace to find variables that they are supposed to use. If the names of those variables are absent, the function looks in the global namespace. If nothing is there, the function looks in the built-in namespace.

f) Using import * is discouraged because it can create name conflicts. But it is convenient because we don't need to always write the module's name before its attributes. What is the issue with import * from a namespace point of view? What kind of namespace does import * create or not create?

Gasp! Thank for any brief correction.
bytecrunch
Reply
#2
import creates variables.

import tkinter creates a variable named "tkinter" that references a module object.
Output:
>>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> import tkinter >>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'tkinter']
from tkinter import Button creates a variable named "Button" that references the tkinter.Button class.
Output:
>>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> from tkinter import Button >>> dir() ['Button', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
from tkinter import * creates 136 variables.
Output:
>>> len(dir()) 7 >>> from tkinter import * >>> len(dir()) 143
The problem using "from tkinter import *" is that you don't know all the variables you just created. If you use "from module import *" for all modules it is likely that importing one module will modify some of the variables created by an earlier module import and there is no easy way for you to know which variables these are. tkinter.ttk actually depends on this confusion, but in general it is a bad thing. You might think you are creating a tkinter object but instead are creating a numpy object or a matplotlib object because of a name collision that invisibly happened during import.
I do not thing this is a good description of "namespace"
Quote:Thanks to namespaces, the Python interpreter can access variables, classes, et.c and avoid naming conflicts. Each namespace is essentially a different symbol table/name table. Functions have their own private namespace (local namespace), modules have their namespace (module namespace), the standard library has its own namespace (built-in namespace), and the running Python program has also its namespace (global namespace).
I think you are mixing two different things together and calling them something that really isn't a thing in Python. Well, that depends on your point of view.

One thing is for sure. Python doesn't have namespaces in the way C++ has namespaces. I cannot tell Python to look for for variables in A, then look in B, and finally in C. Python does that, but I have no control over what "namespace" is A, B or C. Python has scopes, and they are kind of "namespacey". Variables referenced inside a function first look for the variable in the function scope (namespace) then in the enclosed scope (namespace) then the module scope (namespace) and finally in the builtin scope (namespace). So you could say Python has namespaces, but the namespaces are fixed.

The way you are talking about "namespace" also sounds like "attributes". This creates a variable "tkinter" and assigns it to a module object.
import tkinter
print(tkinter)
Output:
<module 'tkinter' from 'C:\\Program Files\\Python38\\lib\\tkinter\\__init__.py'>
The tkinter object module has a bunch of attributes. One of these is Button, which references the tkinter.Button class.
print(tkinter.button)
Output:
<class 'tkinter.Button'>
The import did not create a "namespace", it created an object and a variable. I use the variable to give me access to the object's attributes, but that is not really a "namespace".
bytecrunch likes this post
Reply
#3
Thank you! Lots of stuff I didn't know.
Reply
#4
(Feb-20-2022, 09:33 PM)deanhystad Wrote: import creates variables.

import tkinter creates a variable named "tkinter" that references a module object.
Output:
>>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> import tkinter >>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'tkinter']
from tkinter import Button creates a variable named "Button" that references the tkinter.Button class.
Output:
>>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> from tkinter import Button >>> dir() ['Button', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
from tkinter import * creates 136 variables.
Output:
>>> len(dir()) 7 >>> from tkinter import * >>> len(dir()) 143
The problem using "from tkinter import *" is that you don't know all the variables you just created. If you use "from module import *" for all modules it is likely that importing one module will modify some of the variables created by an earlier module import and there is no easy way for you to know which variables these are. tkinter.ttk actually depends on this confusion, but in general it is a bad thing. You might think you are creating a tkinter object but instead are creating a numpy object or a matplotlib object because of a name collision that invisibly happened during import.
I do not thing this is a good description of "namespace"
Quote:Thanks to namespaces, the Python interpreter can access variables, classes, et.c and avoid naming conflicts. Each namespace is essentially a different symbol table/name table. Functions have their own private namespace (local namespace), modules have their namespace (module namespace), the standard library has its own namespace (built-in namespace), and the running Python program has also its namespace (global namespace).
I think you are mixing two different things together and calling them something that really isn't a thing in Python. Well, that depends on your point of view.

One thing is for sure. Python doesn't have namespaces in the way C++ has namespaces. I cannot tell Python to look for for variables in A, then look in B, and finally in C. Python does that, but I have no control over what "namespace" is A, B or C. Python has scopes, and they are kind of "namespacey". Variables referenced inside a function first look for the variable in the function scope (namespace) then in the enclosed scope (namespace) then the module scope (namespace) and finally in the builtin scope (namespace). So you could say Python has namespaces, but the namespaces are fixed.

The way you are talking about "namespace" also sounds like "attributes". This creates a variable "tkinter" and assigns it to a module object.
import tkinter
print(tkinter)
Output:
<module 'tkinter' from 'C:\\Program Files\\Python38\\lib\\tkinter\\__init__.py'>
The tkinter object module has a bunch of attributes. One of these is Button, which references the tkinter.Button class.
print(tkinter.button)
Output:
<class 'tkinter.Button'>
The import did not create a "namespace", it created an object and a variable. I use the variable to give me access to the object's attributes, but that is not really a "namespace".

Hello deanhystad,

Hope all is well. Thank you for our help with this thread on namespaces and scope. I have just re-read your replies and I still have some small doubts. Would it be ok if I asked you some more questions/clarifications? I feel like I am almost there. Thank you very much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  my python intrepeter completely broke Underscore 9 3,718 Oct-12-2021, 06:57 PM
Last Post: deanhystad
  Modules and namespaces (again?) ptrivino 1 1,820 Oct-24-2020, 10:37 PM
Last Post: Larz60+
  Completely new to coding - quick question Oster22 1 2,673 Jun-19-2018, 08:42 PM
Last Post: Larz60+
  Parse XML with Namespaces dwill 7 20,150 Apr-12-2018, 09:22 PM
Last Post: dwill
  im completely stuck (webcrawler) TatsuyaHiroki 1 3,225 Apr-20-2017, 05:55 PM
Last Post: Larz60+
  namespaces in comprehension in exec() Skaperen 4 4,640 Mar-28-2017, 03:31 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020