在Python中,一个文件称为
module.一个模块可以由多个类或函数组成.
由于Python不仅仅是一种OO语言,所以没有规则说明,一个文件只能包含一个类.
一个文件(模块)应该包含属于一起的类/功能,即提供类似的功能或相互依赖.
当然你不应该夸大这个.如果您的模块由太多的类或功能组成,可读性真的会受到影响.那么现在可能是将功能重新分组到不同的模块中并创建packages.
对于命名约定,您可能需要阅读PEP 8,但简言之:
Class Names
Almost without exception, class names use the CapWords convention.
Classes for internal use have a leading underscore in addition.
和
Package and Module Names
Modules should have short, all-lowercase names. Underscores can be used
in the module name if it improves readability. Python packages should
also have short, all-lowercase names, although the use of underscores is
discouraged.
Since module names are mapped to file names, and some file systems are
case insensitive and truncate long names, it is important that module
names be chosen to be fairly short — this won’t be a problem on Unix,
but it may be a problem when the code is transported to older Mac or
Windows versions, or DOS.
要实例化对象,您必须在文件中导入类.例如
>>> from mymodule import MyClass
>>> obj = MyClass()
要么
>>> import mymodule
>>> obj = mymodule.MyClass()
要么
>>> from mypackage.mymodule import MyClass
>>> obj = MyClass()
你正在询问基本的基本东西,所以我建议阅读tutorial.