Is there a point to import the same module in two different ways in a program?

后端 未结 4 1174
礼貌的吻别
礼貌的吻别 2021-01-23 00:19

Is there a point to import as both wildcard and non-wildcard manner like:

import spam as sp
from spam import *

in the very same file?<

4条回答
  •  野的像风
    2021-01-23 01:12

    When you import spam as sp, you make sure that there are no conflicts with other import commands:

    import spam as sp
    import myfunctions as my
    
    sp.foo()
    my.foo()
    

    This is working as expected, but this isn't:

    from spam import *
    from myfunctions import *
    
    
    foo()
    foo() #Which foo() is meant? UNCLEAR!!!
    

    When you avoid this problem by using import spam as sp, why would you want to use from spam import * anyways? I don't think there is any point in this.

提交回复
热议问题