Assume I have the following package structure:
A/
__init__.py
B.py
C.py
Test.py
Now I am wondering what would be the difference between the following two lines of code:
from A.B import *
import A.B
I know the first line will import everything from the B.py but then what is the point of the second line if it does not import the content of the B.py, when do I need to use the second line instead of the first one?
CodePudding user response:
The difference is in the name binding. from A.B import *
will import everything in B into the current module as a top-level name. import A.B
will import the A.B
module as A.B
.
The second one is generally considered superior because anything you use from A.B
later in the file will be prefixed with A.B
, making it obvious where it was imported from. With an import *
statement you can't tell at a glance what you're importing or what name it will be imported under.
CodePudding user response:
There's no difference, but notice that:
from module import *
functionComingFromModule()
import module
module.functionComingFromModule()
That's the difference.