“ {from module.foo import bar”和“ import module.foo.bar as bar”之间有区别吗?

时间:2018-11-08 19:13:10

标签: python import

使用程序包布局here

sound/                          Top-level package
      __init__.py               Initialize the sound package
      formats/                  Subpackage for file format conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py
              ...
      effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py
              ...
      filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py
              ...

似乎我可以同时做:

from sound.effects import echo

import sound.effects.echo as echo

这两种语法有什么区别?


编辑。我发现的区别是:

import sound.effects
del sound.effects
from sound.effects import echo              # ok
from sound.effects import echo as echo      # ok
import sound.effects.echo                   # ok
import sound.effects.echo as echo           # fail

这已在python 3.6上进行了测试;在python 3.7上一切正常;

0 个答案:

没有答案