日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

python中文件分类_Python中的类是否在不同的文件中?

發(fā)布時(shí)間:2025/4/16 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中文件分类_Python中的类是否在不同的文件中? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。