命名类与在python中命名其文件之间的关联(约定?) [英] Association between naming classes and naming their files in python (convention?)

查看:261
本文介绍了命名类与在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:
    ...

一个类应该放在一个文件中,该文件的名称与该类相同.在此示例中,它将是文件foobar.py.如果我想将类foo导入到某个地方,我必须这样做:

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

这个约定使我有些困惑.我的直觉告诉我,如果文件名指示一个类,则也应该使用大写的首字母,例如FooBar.py.文件名看起来不太漂亮.也许有人可以告诉我什么是标准约定?

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. :-)

推荐答案

您呈现的是标准约定.

包和模块名称

模块应使用简短的全小写名称.下划线可以 如果它提高了可读性,则在模块名称中使用. Python包 也应该使用短的全小写名称,尽管使用下划线 灰心丧气.

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.

由于模块名称已映射到文件名,而某些文件系统为 不区分大小写并截断长名称,该模块很重要 名称要选择的简短些-在Unix上不会有问题, 但是将代码传输到较旧的Mac或 Windows版本或DOS.

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.

当用C或C ++编写的扩展模块具有随附的Python时 提供更高级别(例如,面向对象)的模块 界面上,C/C ++模块的下划线(例如_socket)位于开头.

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).

几乎没有例外,类名使用CapWords约定. 内部使用的类还带有一个下划线.

Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.

( Python样式指南)

例如参见

from configparser import ConfigParser

(顺便说一句,它是Python 2.x中的ConfigParser,但在3.x中更改为小写).

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

这篇关于命名类与在python中命名其文件之间的关联(约定?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆