python 中命名类和命名其文件之间的关联(约定?)
在Python(和其他一些语言)中,我了解到,类的名称应该用小写字母书写,每个单词的第一个字母应该是大写字母。示例:
class FooBar:
...
一个类应该放在一个文件中,名称与该类相同。在此示例中,它将是一个文件 foobar.py。如果我想在某个地方导入类 foo ,我必须这样做:
from foobar import FooBar
这个约定让我有点困惑。我的直觉告诉我,如果文件名表示一个类,那么它的第一个字母也应该大写,就像 FooBar.py 一样。这在文件名中看起来不太漂亮。也许有人可以告诉我这方面的标准约定是什么?
我希望我的问题可以被理解。 :-)
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 of each word, 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, it should be written with the first letter in capitals, too, like FooBar.py
. This doesn'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. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所呈现的是标准约定。
(Python 风格指南)
参见例如
(顺便说一句,它是 Python 2.x 中的 ConfigParser,但已更改在 3.x 中为小写)。
What you have presented is the standard convention.
(Python Style Guide)
See e.g.
(which, incidentally, was ConfigParser in Python 2.x but changed to be lowercase in 3.x).
PEP 8 说:
我还要指出的是,每个文件不一定只有一个类。相反,您应该将相关的类一起包含在同一个文件中。 (当然,在某些情况下,将一个类放入一个文件中是可行的,但情况并非总是如此)
PEP 8 says:
I'll also note that you shouldn't necessarily have on only one class per file. Rather you should include related classes together in the same file. (Of course in some cases, having one class to a file works, but that is not always the case)