-1

I feel like this is one of those questions that there's something I need to learn before I ask a question that makes sense, yet here we are.

So what's the difference with the code below? I understand the syntaxes changes but anything else? Is it just a matter of preference or what?

example 1:

import tkinter as tk
root = tk.Tk()
myLabel = tk.Label(root, text="Automation Panel")

myLabel.pack()
root.mainloop()

example 2:

from tkinter import * 
root = tk()
mLabel = Label(root, text="Automation Panel")

myLabel.pack() 
root.mainloop()

1 Answers1

0

You should avoid . . . import * in the majority of cases. It "dumps" all available names from the module into your current namespace; "polluting" it.

Say you have a module your_module.py:

def func():
    pass

Then you do:

from your_module import *

def func():  # Name collision!
    pass

Now there are two funcs here, the one imported, and the one you defined. Your definition will shadow the imported name though, so it won't be accessible.

Compare that to:

import your_module as ym

def func():  # No name collision
    pass

Since the your_module.func is tucked inside ym, both ym.func and func will be accessible.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117