Sujet : Re: Imports and dot-notation
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 09. Aug 2023, 17:42:21
Autres entêtes
Organisation : Stefan Ram
Message-ID : <imports-20230809172809@ram.dialup.fu-berlin.de>
References : 1
Oliver Schinagl <oliver+
python@schinagl.nl> writes:
Simply put, which of the two would be considered cleaner and
more pythonic?
I'm not sure what "clean" or "pythonic" means here.
To me, the canonical import is
import math
print( math.sin( 0 ))
, because "math.sin" shows where "sin" does come from.
This is valid for production code.
For tiny code examples it might look nicer to use
from math import sin
print( sin( 0 ))
, but I would try to avoid
from math import sin as sine
print( sine( 0 ))
because it hides the common name of the object.
When there is a nested module,
import tkinter.messagebox
tkinter.messagebox.showinfo( "ok.", "It's alright." )
, as this won't work:
import tkinter
tkinter.messagebox.showinfo( "ok.", "It's alright." )
. But for small code examples:
from tkinter.messagebox import showinfo
showinfo( "ok.", "It's alright." )
.