python中文件分类_Python中的类是否在不同的文件中?
在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.
總結
以上是生活随笔為你收集整理的python中文件分类_Python中的类是否在不同的文件中?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ole db 访问接口 sqlncli
- 下一篇: ad域帐号登录提示无法处理请求_面试官: