\r\n
kbd> Странно сказать, я помню это, потому что это противоположность пишущей машинке , которую я использовал.
Ну, если бы это было нормально, мне не нужно было бы помнить это ... :-)
* sup> Изображение из Википедии sup>
В пишущей машинке, когда вы заканчиваете оцифровку строки, вы используете рычаг возврата каретки , который до того, как катит барабан, перевод строки , и Позволяет вручную управлять возвратом каретки .
Вы можете прослушать из этой записи на freesound.org звук подачи бумаги в начале и примерно через -1: 03 секунды после конца, после предупреждения о звонке Звук конца строки барабана, который катится и после того, как возврат каретки.
List all python (.py
) files in the current folder and put them as __all__
variable in __init__.py
from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
Обновление в 2017 году: вы, вероятно, захотите вместо этого использовать importlib
.
Сделайте каталог Foo пакетом, добавив __ init __. Py
. В этом __ init __. Py
add:
import bar
import eggs
import spam
Поскольку вы хотите, чтобы он был динамическим (что может быть, а может и не быть хорошей идеей), перечислите все py-файлы с помощью list dir и импортируйте их примерно так:
import os
for module in os.listdir(os.path.dirname(__file__)):
if module == '__init__.py' or module[-3:] != '.py':
continue
__import__(module[:-3], locals(), globals())
del module
Затем из вашего кода сделайте следующее:
import Foo
Теперь вы можете получить доступ к модулям с помощью
Foo.bar
Foo.eggs
Foo.spam
и т.д. from Foo import *
не является хорошей идеей по нескольким причинам, включая конфликты имен и затруднение анализа кода.
See that your __init__.py
defines __all__
. The modules - packages doc says
The
__init__.py
files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case,__init__.py
can just be an empty file, but it can also execute initialization code for the package or set the__all__
variable, described later....
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. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package. For example, the filesounds/effects/__init__.py
could contain the following code:
__all__ = ["echo", "surround", "reverse"]
This would mean that
from sound.effects import *
would import the three named submodules of the sound package.
Add the __all__
Variable to __init__.py
containing:
__all__ = ["bar", "spam", "eggs"]