Oct-07-2022, 05:44 PM
(Feb-20-2022, 09:33 PM)deanhystad Wrote: import creates variables.
import tkinter creates a variable named "tkinter" that references a module object.
from tkinter import Button creates a variable named "Button" that references the tkinter.Button class.
Output:>>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> import tkinter >>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'tkinter']
from tkinter import * creates 136 variables.
Output:>>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> from tkinter import Button >>> dir() ['Button', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
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.
Output:>>> len(dir()) 7 >>> from tkinter import * >>> len(dir()) 143
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)The tkinter object module has a bunch of attributes. One of these is Button, which references the tkinter.Button class.
Output:<module 'tkinter' from 'C:\\Program Files\\Python38\\lib\\tkinter\\__init__.py'>
print(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".
Output:<class 'tkinter.Button'>
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!