python中文件分类_Python中的类是否在不同的文件中?
在Python中,一個(gè)文件稱(chēng)為
module.一個(gè)模塊可以由多個(gè)類(lèi)或函數(shù)組成.
由于Python不僅僅是一種OO語(yǔ)言,所以沒(méi)有規(guī)則說(shuō)明,一個(gè)文件只能包含一個(gè)類(lèi).
一個(gè)文件(模塊)應(yīng)該包含屬于一起的類(lèi)/功能,即提供類(lèi)似的功能或相互依賴(lài).
當(dāng)然你不應(yīng)該夸大這個(gè).如果您的模塊由太多的類(lèi)或功能組成,可讀性真的會(huì)受到影響.那么現(xiàn)在可能是將功能重新分組到不同的模塊中并創(chuàng)建packages.
對(duì)于命名約定,您可能需要閱讀PEP 8,但簡(jiǎn)言之:
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.
要實(shí)例化對(duì)象,您必須在文件中導(dǎo)入類(lèi).例如
>>> from mymodule import MyClass
>>> obj = MyClass()
要么
>>> import mymodule
>>> obj = mymodule.MyClass()
要么
>>> from mypackage.mymodule import MyClass
>>> obj = MyClass()
你正在詢(xún)問(wèn)基本的基本東西,所以我建議閱讀tutorial.
總結(jié)
以上是生活随笔為你收集整理的python中文件分类_Python中的类是否在不同的文件中?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ole db 访问接口 sqlncli
- 下一篇: python输出等腰三角形代码_酷呆了!