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...
#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


Messages In This Thread
RE: Still not completely clear about namespaces... - by bytecrunch - Oct-07-2022, 05:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  my python intrepeter completely broke Underscore 9 3,845 Oct-12-2021, 06:57 PM
Last Post: deanhystad
  Modules and namespaces (again?) ptrivino 1 1,867 Oct-24-2020, 10:37 PM
Last Post: Larz60+
  Completely new to coding - quick question Oster22 1 2,706 Jun-19-2018, 08:42 PM
Last Post: Larz60+
  Parse XML with Namespaces dwill 7 20,454 Apr-12-2018, 09:22 PM
Last Post: dwill
  im completely stuck (webcrawler) TatsuyaHiroki 1 3,268 Apr-20-2017, 05:55 PM
Last Post: Larz60+
  namespaces in comprehension in exec() Skaperen 4 4,734 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