python 我应该在自己的 .py 文件中创建每个类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2098088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:46:30  来源:igfitidea点击:

Should I create each class in its own .py file?

python

提问by Sergio Tapia

I'm quite new to Python in general.

总的来说,我对 Python 很陌生。

I'm aware that I can create multiple classes in the same .py file, but I'm wondering if I should create each class in its own .py file.

我知道我可以在同一个 .py 文件中创建多个类,但我想知道是否应该在自己的 .py 文件中创建每个类。

In C# for instance, I would have a class that handles all Database interactions. Then another class that had the business rules.

例如,在 C# 中,我将有一个处理所有数据库交互的类。然后是另一个具有业务规则的类。

Is this the case in Python?

在 Python 中是这种情况吗?

采纳答案by Thomas Wouters

No. Typical Python style is to put related classes in the same module. It may be that a class ends up in a module of its own (especially if it's a large class), but it should not be a goal in its own right. And when you do, please do not name the module after the class -- you'll just end up confusing yourself and others about which is which.

不是。典型的 Python 风格是将相关的类放在同一个模块中。一个类可能会在它自己的模块中结束(特别是如果它是一个大类),但它本身不应该是一个目标。当你这样做时,请不要在课程后命名模块——你最终会让自己和其他人混淆哪个是哪个。

回答by richo

Each .py file represents a module, so you should keep logical groups of functions, constants and classes together in the same file.

每个 .py 文件代表一个模块,因此您应该将函数、常量和类的逻辑组放在同一个文件中。

Each class in a .py file will just create epic bloat in your module table, since if you're only interested in one class you can still

.py 文件中的每个类只会在你的模块表中创建史诗般的膨胀,因为如果你只对一个类感兴趣,你仍然可以

from whatever import SomeClass

回答by Nathan Farrington

I'll disagree with the others and say yes. For me, I have had better success putting each class in its own file (module). But there are exceptions so let me explain with an example.

我会不同意其他人的意见并说是的。对我来说,我把每个类放在自己的文件(模块)中取得了更好的成功。但也有例外,所以让我用一个例子来解释。

If you have a class Foo, then put it in a file called Foo.py, with the following sections:

如果你有一个 Foo 类,那么把它放在一个名为 Foo.py 的文件中,包含以下部分:

  1. Imports
    • This is where you pull in dependencies.
    • Examples: import math, from Bar import *
  2. Globals
    • This is where you define the external interface to your module, which are all of the symbols that are visible outside of this module.
    • Example: __all__ = ['Foo']
    • This is also where you can define global variables (bad) and global constants (good). These globals need not be exported; they can be made global merely to simplify the code.
    • Example: PI = 3.14159means that you can write PI, whereas if you defined it inside class Foo then you would need to write Foo.PI.
  3. Functions
    • This is where you define all top-level functions that are relevant to class Foo, but don't belong in the class Foo namespace. These are probably rare since classes allow both @staticmethodsand inner classes.
    • Example: def print_foo(foo): print(foo)
  4. Classes
    • Example: class Foo(object): pass
  1. 进口
    • 这是您引入依赖项的地方。
    • 例子:import math,from Bar import *
  2. 全局变量
    • 这是您定义模块的外部接口的地方,它是该模块外部可见的所有符号。
    • 例子: __all__ = ['Foo']
    • 这也是您可以定义全局变量(坏)和全局常量(好)的地方。这些全局变量不需要导出;它们可以是全局的,只是为了简化代码。
    • Example:PI = 3.14159意味着你可以写PI,而如果你在类 Foo 中定义它,那么你需要写Foo.PI.
  3. 职能
    • 您可以在此处定义与类 Foo 相关但不属于类 Foo 命名空间的所有顶级函数。这些可能很少见,因为类同时允许@staticmethods内部类。
    • 例子: def print_foo(foo): print(foo)
  4. 班级
    • 例子: class Foo(object): pass

Sometimes you will want to place more than one class in the same module. You should do this whenever two or more classes are conceptually related to the point where you would almost always use them together and never independently. This is the exception, not the norm. In this case add all of the class names to the __all__global.

有时您会希望在同一个模块中放置多个类。每当两个或多个类在概念上与您几乎总是一起使用它们而不是独立使用它们的点相关时,您应该这样做。这是例外,而不是常态。在这种情况下,将所有类名添加到__all__全局。

Finally, for every module Foo.py, there should be a corresponding unit test module called testFoo.py.

最后,对于每个模块 Foo.py,都应该有一个相应的单元测试模块,称为 testFoo.py。

回答by Bruno Oliveira

Another point worth mentioning, is that if a file grows too large, you can always transform it into a package, making easy to reorganize without breaking the client's code.

另一点值得一提的是,如果文件变得太大,您可以随时将其转换为包,从而在不破坏客户端代码的情况下轻松重新组织。

回答by Jim Paul

Yes each class in its own file. Importing even a single class (or function) in a file with multiple classes causes python to execute the definition of all classes in the file. Try this:

是的,每个类都在它自己的文件中。即使在具有多个类的文件中导入单个类(或函数)也会导致 python 执行文件中所有类的定义。试试这个:

manyClass.py

许多类.py

class foo():
   print 'a bunch of time consuming work'

class tryme():
   print 'try me'

Now type this in the interpreter shell...

现在在解释器 shell 中输入这个...

from manyClasses import tryme

从 manyClasses 导入 tryme

a bunch of time consuming work
try me

一堆耗时的工作
试试我

回答by Triptych

Probably not. Python files are "modules". Modules should contain just what code is independently reusable. If that comprises several classes, which is the norm, then that's perfectly ok.

可能不是。Python 文件是“模块”。模块应该只包含可独立重用的代码。如果这包括几个类,这是常态,那完全没问题。