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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值