`from x import y` vs. `from x.y import *`

前端 未结 2 727
迷失自我
迷失自我 2021-01-02 21:33

What is the difference between these two lines?

from PyQt4 import QtGui
from PyQt4.QtGui import *

The first line is \"import QtGui class fr

2条回答
  •  -上瘾入骨i
    2021-01-02 22:25

    First statement imports the specified module into the current namespace.
    Second statement imports everything from the specified module into the current namespace.

    So 1) means you still need to explicitly reference any classes/functions etc through the module namespace
    2) Means you don't

    Here's a compare and contrast that shows the difference

    1)

    import math
    
    d = math.sqrt(10)
    

    2)

    from math import *
    
    d = sqrt(10)
    

    Note that you can choose to import a specific symbol from a module if you want i.e.

    from math import sqrt
    d = sqrt(10)
    

提交回复
热议问题