The google python style guide mentions at 2.2 that Use import statements for packages and modules only, not for individual classes or functions.
.
Why is importing functions or classes bad practice? What errors could it lead to?
There is typically relatively small set of modules available to import, but they contain hundreds of classes. Module names are usually unique, class names are much less likely to do so. Repeated imports of identically named class will silently overwrite them and cause bugs when trying to use them.
from colorama import Style
from tkinter.ttk import Style
print(type(Style())) # this will always be tkinter.ttk.Style now, colorama.Style is unaccessible
When you copy a snippet of the code if names include a module name it is much easier to identify which module various things come from. Consider the following:
window = Tk()
label1 = Label(window, text="some text", style = "s.TLabel")
label1.grid()
window.mainloop()
Ok, we see Tk()
, so Label is probably from this as well - let's do from tkinter import *
and be done.
TclError: unknown option "-style"
?
Yeah, it's because the code had tkinter.ttk.Label
in mind, not tkinter.Label