Python class 个组织的代码结构
Python code structure for class organization
在 Java 中,代码以包的形式构建,每个 class 在一个单独的文件中。 python有没有类似的做法?将每个 class 放在不同的 python 文件中并让它们相互导入是否更好,还是我应该将我的代码(我所有的 classes)转储到一个文件中?
In Java the code is structured in packages with you each class in a separate file. Is there any similar practice in python?
绝对不会。实际上,Python 不会强制您将所有代码放入 classes - 普通函数也可以 - 所以即使是“每个 class” 前提也没有意义。
Is it better to have each class in a different python file
绝对不是 - 这只会让您的代码成为维护的噩梦。
or should I just dump my code(all my classes) in a single file?
都没有(除非它是一个非常小的应用程序)。您想以内聚的、解耦的 modules/packages 重新组合您的代码(函数、classes 等),这无论如何是所有语言中已知的最佳实践。如果您有一个包含域代码、持久性和 UI 的“完整”应用程序,您可能希望将其用作您的第一级包。
当您在 Python 中构建代码时,根据名称空间(从名称到对象的映射)来思考是很有用的:https://docs.python.org/3/tutorial/classes.html .
然后你可以在模块(包含 Python 定义和语句的文件)中构建你的代码(取决于他的复杂性),然后打包(一种构建 Python 模块命名空间的方法,使用“点模块名称”):https://docs.python.org/3/tutorial/modules.html
默认情况下python没有为代码文件中的每个class创建一个文件。
要为每个 class 创建单独的文件,每个 class 的代码应该在单独的文件中
如果两个 class 相关或紧密耦合,客户端代码将需要实例化每个,最好将它们放在同一个文件中。
如果一个父 class 有多个子 class,而子 class 的代码很少,最好放在一个文件中。
这取决于使用 case/how 你想要构造你的 code/application。
这是 classes 文档的第一段摘录:
Compared with other programming languages, Python’s class mechanism
adds classes with a minimum of new syntax and semantics. It is a
mixture of the class mechanisms found in C++
and Modula-3
. Python
classes provide all the standard features of Object Oriented
Programming: the class inheritance mechanism allows multiple base
classes, a derived class can override any methods of its base class or
classes, and a method can call the method of a base class with the
same name. Objects can contain arbitrary amounts and kinds of data. As
is true for modules, classes partake of the dynamic nature of Python:
they are created at runtime, and can be modified further after
creation.
基本上在Python、"Everything is an Object"cough cough包括classes.
话虽如此,如果您进一步阅读文档,在某些用例中您可能希望在同一文件中包含多个 class,例如 exceptions 因为他们"are classes too"
class ApplicationError(Exception):
pass
class LoadingError(Exception):
pass
class ValidationError(ApplicationError):
pass
像这样的东西可以放在一个文件中,例如errors.py
或者如果您有一个 class 覆盖另一个,例如:
class Dog:
def __init__(self, name, weight):
self.name = name
self.weight = weight
def bark():
return("Bark")
class Chihuahua(Dog):
def bite_ankles():
return("Pain")
这样的东西可以在一个名为 breeds.py
.
的文件中
我现在所做的基本上是 2 python modules 这些有助于将 python 应用程序分解成小块。
As your program gets longer, you may want to split it into several
files for easier maintenance. You may also want to use a handy
function that you’ve written in several programs without copying its
definition into each program.
Python has a way to put definitions in a file and use them in a script
or in an interactive instance of the interpreter. Such a file is
called a module; definitions from a module can be imported into other
modules or into the main module (the collection of variables that you
have access to in a script executed at the top level and in calculator
mode).
A module is a file containing Python definitions and statements. The
file name is the module name with the suffix .py appended.
在某些情况下,在一个文件中使用多个 classes 和定义会很方便,但这取决于您打算如何构建项目。当然,您可以将所有代码放入一个文件中,但这很难管理,并且有悖于 Zen of Python "Readability Counts".
在 Java 中,代码以包的形式构建,每个 class 在一个单独的文件中。 python有没有类似的做法?将每个 class 放在不同的 python 文件中并让它们相互导入是否更好,还是我应该将我的代码(我所有的 classes)转储到一个文件中?
In Java the code is structured in packages with you each class in a separate file. Is there any similar practice in python?
绝对不会。实际上,Python 不会强制您将所有代码放入 classes - 普通函数也可以 - 所以即使是“每个 class” 前提也没有意义。
Is it better to have each class in a different python file
绝对不是 - 这只会让您的代码成为维护的噩梦。
or should I just dump my code(all my classes) in a single file?
都没有(除非它是一个非常小的应用程序)。您想以内聚的、解耦的 modules/packages 重新组合您的代码(函数、classes 等),这无论如何是所有语言中已知的最佳实践。如果您有一个包含域代码、持久性和 UI 的“完整”应用程序,您可能希望将其用作您的第一级包。
当您在 Python 中构建代码时,根据名称空间(从名称到对象的映射)来思考是很有用的:https://docs.python.org/3/tutorial/classes.html .
然后你可以在模块(包含 Python 定义和语句的文件)中构建你的代码(取决于他的复杂性),然后打包(一种构建 Python 模块命名空间的方法,使用“点模块名称”):https://docs.python.org/3/tutorial/modules.html
默认情况下python没有为代码文件中的每个class创建一个文件。
要为每个 class 创建单独的文件,每个 class 的代码应该在单独的文件中
如果两个 class 相关或紧密耦合,客户端代码将需要实例化每个,最好将它们放在同一个文件中。
如果一个父 class 有多个子 class,而子 class 的代码很少,最好放在一个文件中。
这取决于使用 case/how 你想要构造你的 code/application。
这是 classes 文档的第一段摘录:
Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax and semantics. It is a mixture of the class mechanisms found in
C++
andModula-3
. Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain arbitrary amounts and kinds of data. As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation.
基本上在Python、"Everything is an Object"cough cough包括classes.
话虽如此,如果您进一步阅读文档,在某些用例中您可能希望在同一文件中包含多个 class,例如 exceptions 因为他们"are classes too"
class ApplicationError(Exception):
pass
class LoadingError(Exception):
pass
class ValidationError(ApplicationError):
pass
像这样的东西可以放在一个文件中,例如errors.py
或者如果您有一个 class 覆盖另一个,例如:
class Dog:
def __init__(self, name, weight):
self.name = name
self.weight = weight
def bark():
return("Bark")
class Chihuahua(Dog):
def bite_ankles():
return("Pain")
这样的东西可以在一个名为 breeds.py
.
我现在所做的基本上是 2 python modules 这些有助于将 python 应用程序分解成小块。
As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.
Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
在某些情况下,在一个文件中使用多个 classes 和定义会很方便,但这取决于您打算如何构建项目。当然,您可以将所有代码放入一个文件中,但这很难管理,并且有悖于 Zen of Python "Readability Counts".