類的 Python 模塊結構 (Python module structure for classes)


問題描述

類的 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 srbTalendarghchoiPetar)

參考文件

  1. Python module structure for classes (CC BY‑SA 2.5/3.0/4.0)

#Python #module #class






相關問題

如何從控制台中導入的文件中訪問變量的內容? (How do I access the contents of a variable from a file imported in a console?)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

為 KeyError 打印出奇怪的錯誤消息 (Strange error message printed out for KeyError)

django 1.9 中的 from django.views.generic.simple import direct_to_template 相當於什麼 (What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9)

查詢嵌入列表中的數組 (Querying for array in embedded list)

如何在 Python 中搜索子字符串是否在二進製文件中? (How to search if a substring is into a binary file in Python?)

為什麼要避免 while 循環? (Why avoid while loops?)

使用python的json模塊解析json請求 (Parse a json request using json module of python)

為什麼使用 py2app 模塊創建 mac 文件時出現錯誤? (Why i am getting Error when creating mac file using py2app module?)

當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)

如何繪製一條帶斜率和一個點的線?Python (How to plot a line with slope and one point given? Python)

Pickle 找不到我不使用的模塊? (Pickle can't find module that I am not using?)







留言討論