問題描述
類的 Python 模塊結構 (Python module structure for classes)
我目前正在按以下方式構建一個項目:
.
├── example_app
│ ├── __init__.py
│ ├── app.py
│ ├── requirements.txt
│ └── classes
│ ├── ExampleClass1.py
│ ├── ExampleClass2.py
│ ├── __init__.py
└── tests
└── test_app.py
在 classes/__init__.py
的內部,我已經為所有人定義了一個基類其他“示例類”的 繼承自。然後,在 app.py
我需要導入所有這些類。然而,這變得非常冗長,因為我必須輸入:
from example_app.classes.ExampleClass1 import ExampleClass1
from example_app.classes.ExampleClass2 import ExampleClass2
...
有沒有辦法以更好的方式來構建它?理想情況下,我想將每個類保存在一個單獨的文件中,因為它們彼此並不相似。我想導入 classes/__init.py
中的所有類,但這似乎不對。
參考解法
方法 1:
In my opinion, importing all the classes you want to expose from the subpackage classes
inside its __init__.py
is the way to go. In fact, that's what many top‑tier Python libraries/frameworks do (see TensorFlow/Keras layers
subpackage here and Python's multiprocessing
subpackage here).
When doing that, however, you should probably follow Google Python Style Guide and:
Use import statements for packages and modules only, not for individual classes or functions. Note that there is an explicit exemption for imports from the typing module.
In your case, it would translate to this:
example_app/classes/__init__.py
:
from example_app.classes.ExampleClass1 import ExampleClass1
from example_app.classes.ExampleClass2 import ExampleClass2
example_app/__init__.py
:
# ...
from example_app import classes
# ...
Some other .py
:
from example_app import classes
ex1_instance = classes.ExampleClass1()
Or even better:
import example_app as ex
ex1_instance = ex.classes.ExampleClass1()
方法 2:
You can treat a module as a class. If your module ExampleClassN.py
is only to define a class ExampleClassN
, just add the following two line at the end of each module.
import sys
sys.modules[__name__] = ExampleClassN # N = 1, 2, ...
Then you can
from example_app.classes import ExampleClass1
from example_app.classes import ExampleClass2
...
or
from example_app.classes import (
ExampleClass1,
ExampleClass2,
...
)
or
from example_app.classes import *
方法 3:
First off, the __init__.py
shouldn't contain your base class. It is better to have separate file for the base class, as __init__.py
usually serves as initializaion of the module.
What you can do is following:
use
__init__.py
to import all of the classes from the submodules. Eg.import ExampleClass1.ExampleClass1 import ExampleClass2.ExampleClass2 __all__ = ['ExampleClass1','ExampleClass2']
Finally in order to use the wildcard operator, e.g. from classes import *
you need to add the following line
__all__ = ['ExampleClass1','ExampleClass2']
(by srb、Talendar、ghchoi、Petar)