简体   繁体   English

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

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

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. 例如,在C#中,我将拥有一个处理所有数据库交互的类。 Then another class that had the business rules. 然后是另一个具有业务规则的类。

Is this the case in Python? 在Python中是这种情况吗?

No. Typical Python style is to put related classes in the same module. 否。典型的Python风格是将相关类放在同一模块中。 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. 而且,当您这样做时,请不要在课程后给模块命名,否则最终会使自己和其他人混淆哪个是哪个。

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

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 * 示例: from Bar import * 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'] 例如: __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.14159 means that you can write PI , whereas if you defined it inside class Foo then you would need to write Foo.PI . 示例: PI = 3.14159意味着您可以编写PI ,而如果在类Foo中定义了PI ,则需要编写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. 在这里定义所有与类Foo相关但不属于类Foo命名空间的顶级函数。 These are probably rare since classes allow both @staticmethods and inner classes. 由于类同时允许使用@staticmethods和内部类,因此它们可能很少见。
    • Example: def print_foo(foo): print(foo) 示例: def print_foo(foo): print(foo)
  4. Classes 班级
    • Example: class Foo(object): pass 示例: 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__全局__all__

Finally, for every module Foo.py, there should be a corresponding unit test module called testFoo.py. 最后,对于每个模块Foo.py,都应该有一个名为testFoo.py的相应单元测试模块。

值得一提的另一点是,如果文件太大,则始终可以将其转换为程序包,从而易于重组而不会破坏客户端的代码。

Probably not. 可能不会。 Python files are "modules". Python文件是“模块”。 Modules should contain just what code is independently reusable. 模块应包含可独立重用的代码。 If that comprises several classes, which is the norm, then that's perfectly ok. 如果这包含几个类,这是正常的,那完全可以。

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. 在具有多个类的文件中甚至导入单个类(或函数),都会导致python执行文件中所有类的定义。 Try this: 尝试这个:

manyClass.py manyClass.py

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

class tryme():
   print 'try me'

Now type this in the interpreter shell... 现在在解释器外壳中键入此内容...

from manyClasses import tryme 从许多类进口tryme

a bunch of time consuming work 一堆费时的工作
try me 试试我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何将每个工作表分配给它自己的变量? - How should I assign each worksheet to its own variable? 将具有8个以上类的模块拆分为一个包,每个类都在自己的文件中 - Splitting a module with 8+ classes into a package with each class in its own file 制作一个创建自己的 class 的 object 的方法 - Make a method that create an object of its own class 每个Django应用程序都应该有自己的服务器实例吗? - Should each Django app have its own server instance? 如何为每个屏幕添加自己的.py和.kv文件? - How to add for each screen an own .py and .kv file? 我想创建目录并保存图像文件但它的名字已经存在它应该有数字表示 - I want to create directory and save image file but its name already exist its should have numbers indicating 导入 CSV 文件并使每列成为自己的列表 - Import CSV file and make each column its own list 在Python中将文本文件的每个值移至其各自的行 - Moving each value of a text file to its own line in Python 在CSV文件中,每个字母都有自己的列 - In CSV file each letter has its own column 我如何在 tkinter 中创建 4 个三角形,以便它们从屏幕中间开始并且每个三角形都进入自己的角落并消失? - How do I create 4 triangles in tkinter so that they start in the middle of the screen and each one of them goes in its own corner and disappears?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM