python文件名和类名需要一致吗,命名类与在python中命名其文件之间的关联(约定?)...

In python (and some other languages) I have learned, that the name of a class should be written in small letters except for the first letter, which should be a capital letter. Example:

class FooBar:

...

A class should go in a file, named the same as the class. In this example it would be a file foobar.py. If I want to import the class foo somewhere I have to do this:

from foobar import FooBar

This convention confuses me a little. My intuition tells me, that if the filename indicates a class, than it should be written with the first letter in capitals, too, like FooBar.py. This don't look pretty in file names. Perhaps someone could tell me what is the standard convention for this?

I hope I made my question understandable. :-)

解决方案

What you have presented is the standard convention.

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.

When an extension module written in C or C++ has an accompanying Python

module that provides a higher level (e.g. more object oriented)

interface, the C/C++ module has a leading underscore (e.g. _socket).

Class Names

Almost without exception, class names use the CapWords convention.

Classes for internal use have a leading underscore in addition.

See e.g.

from configparser import ConfigParser

(which, incidentally, was ConfigParser in Python 2.x but changed to be lowercase in 3.x).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值