Python: Unterschied zwischen Import Modul und From Modul Import *?

4 Antworten

Vom Beitragsersteller als hilfreich ausgezeichnet

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 module

Pros: 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 foo

Pros: 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()
Woher ich das weiß:eigene Erfahrung