I like the Java convention of having one public class per file, even if there are sometimes good reasons to put more than one public class into a single file. In my case I have alternative implementations of the same interface. But if I would place them into separate files, I'd have redundant names in the import statements (or misleading module names):

我喜欢Java惯例,每个文件有一个公共类,即使有时有很好的理由将多个公共类放入一个文件中。在我的例子中,我有相同接口的其他实现。但是如果我将它们放在单独的文件中,我将在导入语句中有冗余的名称(或者误导模块名):

import someConverter.SomeConverter

whereas someConverter would be the file (and module) name and SomeConverter the class name. This looks pretty inelegant to me. To put all alternative classes into one file would lead to a more meaningful import statement:

而someConverter将是文件(和模块)名和类名的someConverter。这在我看来很不优雅。将所有可选类放在一个文件中将导致更有意义的导入语句:

import converters.SomeConverter

But I fear that the files become pretty large, if I put all related classes into a single module file. What is the Python best practise here? Is one class per file unusual?

但是,如果我将所有相关的类放在一个模块文件中,我担心这些文件会变得非常大。Python在这里的最佳实践是什么?每个文件有一个类不同寻常吗?

2 个解决方案

#1


55

A lot of it is personal preference. Using python modules, you do have the option to keep each class in a separate file and still allow for import converters.SomeConverter (or from converters import SomeConverter)

很多都是个人喜好。使用python模块,您可以将每个类保存在单独的文件中,并且仍然允许导入转换器。一些转换器(或从转换器进口一些转换器)

Your file structure could look something like this:

您的文件结构可以如下所示:

* converters
     - __init__.py
     - baseconverter.py
     - someconverter.py
     - otherconverter.py

and then in your __init__.py file:

然后在你的简历中。py文件:

from baseconverter import BaseConverter
from otherconverter import OtherConverter

更多相关文章

  1. 【python coding 1:网络检测】ping本地文件里的ip地址
  2. 如何输出NLTK块到文件?
  3. python 读写文本文件
  4. 批量重命名文件——python实现
  5. Django:测试成功加载静态文件
  6. 使用python 3.6将多个文件并行加载到内存中的最佳方法是什么?
  7. 在Python 3.x中将多个字典写入多个csv文件
  8. 如何使用python 3检查文件夹是否包含文件
  9. 如何使用未受标头影响的python导入csv文件,其中第一列为非数值

随机推荐

  1. SeekBar圆球显示问题
  2. Android横竖屏要解决的问题
  3. Android NDK环境搭建及sample展示
  4. android 入门xml布局文件
  5. Android设备功能之录音教程篇
  6. adt-bundle和android studio下载地址(不定
  7. android之使用mvn构建创造项目步骤
  8. 常用Android开发组件之图像类组件
  9. Android Studio创建计算器Demo
  10. Android apk静默安装与卸载7.0 9.0