I took a python tutorial a year ago and rather enjoyed it, so I thought I'd attempt to incorporate tkinter widgets into things because, well, GUI. Many of the tutorials I have seen covering beginner tkinter begin the code with the line:
from tkinter import *
Is there a compelling reason for this? I mean, why (or not) load everything?
Asked
Active
Viewed 90 times
2

Moinuddin Quadri
- 46,825
- 13
- 96
- 126

endorpheus
- 111
- 1
- 5
-
1It depends but it does pollute the local namespace. If you want a shorter namespace name for `tkinter` you could `import tkinter as tk`. Then your code would look like `tk.<...>`. – AChampion Nov 09 '16 at 22:44
1 Answers
5
Yes, it is a very BAD practice for two reasons:
- Code Readability
- Risk of overriding the variables/functions etc
For point 1: Let's see an example of this:
from module1 import *
from module2 import *
from module3 import *
a = b + c - d
Here, on seeing the code no one will get idea regarding from which module b
, c
and d
actually belongs.
On the other way, if you do it like:
# v v will know that these are from module1
from module1 import b, c # way 1
import module2 # way 2
a = b + c - module2.d
# ^ will know it is from module2
It is much cleaner for you, and also the new person joining your team will have better idea.
For point 2: Let say both module1
and module2
have variable as b
. When I do:
from module1 import *
from module2 import *
print b # will print the value from module2
Here the value from module1
is lost. It will be hard to debug why the code is not working even if b
is declared in module1
and I have written the code expecting my code to use module1.b
.
If you have same variables in different modules, and you do not want to import entire module, you may even do:
from module1 import b as mod1b
from module2 import b as mod2b

Moinuddin Quadri
- 46,825
- 13
- 96
- 126