Python: Unterschied zwischen Import Modul und From Modul Import *?
Frage steht oben.
Es scheint doch das Gleiche zu sein oder nicht?
4 Antworten
Also wenn du schreibst (empfohlen von mir) :
from tkinter import*
Dann kannst du schreiben:
window = Tk()
Aber wenn du schreibst:
import tkinter
Dann musst du immer schreiben:
window = tkinter.Tk()
https://stackoverflow.com/questions/710551/use-import-module-or-from-module-import
import modulePros: Less maintenance of your import statements. Don't need to add any additional imports to start using another item from the module
Cons: Typing module.foo in your code can be tedious and redundant (tedium can be minimized by using import module as mo then typing mo.foo)
from module import fooPros: Less typing to use foo. More control over which items of a module can be accessed
Cons: To use a new item from the module you have to update your import statement. You lose context about foo. For example, it's less clear what ceil() does compared to Math.ceil()
Überhaupt nicht.
import tkinter
Fenster = tkinter.Tk()
oder
from tkinter import *
Fenster = Tk()
oder(von mir empfohlen
import tkinter as tk
Fenster = tk.Tk()
Es gibt schon Unterschiede, wie hier in der akzeptierten Antwort beschrieben: