你如何PEP 8名称一个类的名称是首字母缩写? [英] How do you PEP 8-name a class whose name is an acronym?

查看:243
本文介绍了你如何PEP 8名称一个类的名称是首字母缩写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试遵守Python代码的样式指南(也称为 PEP 8 )。因此,命名类的首选方法是使用CamelCase:


几乎没有例外,类名
使用CapWords约定。


如果我的类名由两个字符组成,我如何与PEP 8保持一致首字母缩略词(英语应该大写)。例如,如果我的类名是'NASA JPL',你会叫什么名字呢?:

  class NASAJPL 1 
class NASA_JPL():#2
class NasaJpl():#3

我使用#1,但它看起来很奇怪; #3看起来很奇怪,#2似乎违反PEP 8.



想法?



/ p>

非常感谢您的回答。虽然这个问题没有一个正确的答案,我从你的见解中获益良多。



我将使用#3:更好地始终是错误的(缩写为 Title )不一致。



再次感谢。

解决方案

PEP-8 确实涵盖了这一点(至少部分):


注意:在CapWords中使用缩写时,大写缩写的所有字母。因此, HTTPServerError 优于 HttpServerError



b $ b

我会读的意思是 NASAJPL()是根据PEP-8推荐的名字。



个人我会找到 NasaJpl() 最容易扫描,因为大写字母容易标记单词边界并给出命名一个独特的形状。


I try to adhere to the style guide for Python code (also known as PEP 8). Accordingly, the preferred way to name a class is using CamelCase:

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

How can I be consistent with PEP 8 if my class name is formed by two acronyms (which in proper English should be capitalized). For instance, if my class name was 'NASA JPL', what would you name it?:

class NASAJPL():  # 1
class NASA_JPL(): # 2
class NasaJpl():  # 3

I am using #1, but it looks weird; #3 looks weird too, and #2 seems to violate PEP 8.

Thoughts?

EDIT:

Many thanks for your answers. While the question really does not have a 'correct' answer, I benefited a lot from your insights.

I will be resorting to a #3: better to be consistently wrong (in the sense of having an acronym/abbreviation as a Title) than inconsistently right.

Thanks again.

解决方案

PEP-8 does cover this (at least partially):

Note: When using abbreviations in CapWords, capitalize all the letters of the abbreviation. Thus HTTPServerError is better than HttpServerError.

Which I would read to mean that NASAJPL() is the recommended name according to PEP-8.

Personally I'd find NasaJpl() the easiest to scan since the upper case letters easily mark word boundaries and give the name a distinctive shape.

这篇关于你如何PEP 8名称一个类的名称是首字母缩写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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