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

后端 未结 4 1173
礼貌的吻别
礼貌的吻别 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 00:51

    No. Since you have imported spam with name sp and then imported everything using from spam import * sp will never be used and is therefore unnecessary.

    For example if we have a function called somefunction(). import spam as sp would mean we could call somefunction() with sp.somefunction()

    Since from spam import * it can be called directly somefunction() so why use sp.somefunction() instead.

    from spam import * is considered extremely bad practice. You should import each function individually rather than do that. (from spam import somefunction, from spam import someotherfunction and so on). Or you could just use sp.somefunction(), sp.someotherfunction().

提交回复
热议问题