@PyLearner

Почему нельзя импортировать через import *?

Документация https://docs.python.org/3/tutorial/modules.html#pa...
Now what happens when the user writes from sound.effects import *? Ideally, one would hope that this somehow goes out to the filesystem, finds which submodules are present in the package, and imports them all. This could take a long time and importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.

The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered.


Что непонятно:
This could take a long time and importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.

1) Почему это может занять большое количество времени? Что в пакете может быть кроме подпакетов и модулей? Почему питон сам не может прочесть имена папок в пакете и проверить наличие модулей в них?

importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.

2) От того, что я сам впишу имена папок в __init__.py __all__, а не питон сам идентифицирует их по простому алгоритму (папка=пакет, файл .py=модуль) могут появиться какие-то побочные эффекты? Какие?
  • Вопрос задан
  • 232 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы