python 变量名应该使用哪些缩写?
一般来说,我使用 PEP-8 中规定的标准命名变量。喜欢:
delete_projects
connect_server
但是有时我找不到任何好的名称,并且名称只是扩展为很长的名称:
project_name_to_be_deleted
我可以使用 pr_nm_del ,但这使得代码不可读。我真的很难找到好的函数变量名。每当我开始编写新函数时,我都会花时间找到一个好的变量名。
是否有任何标准为众所周知的变量名称选择某些缩写,例如删除、项目、配置等?如何选择简短但良好且可读的变量名称?
这个问题可能不直接依赖于Python,但由于不同的编程语言使用不同的变量名称格式,我想我将这个问题限制为仅限Python。
In generally I'm using the standard naming stated in PEP-8 for variables. Like:
delete_projects
connect_server
However sometimes I can't find any good name and the name just extend to a long one:
project_name_to_be_deleted
I could use pr_nm_del
, but this makes the code unreadable. I'm really suffering finding good variable names for functions. Whenever I begin to write a new function I just spent time to find a good variable name.
Is there any standard for choosing certain abbreviations for well known variable names like, delete,project,configuration, etc.
? How do you choose short but good and readable variable names ?
This question might be not depend directly to Python, but as different programming languages uses different variable names formatting I thought I limit this question to Python only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
pr_nm_del
?你不妨让一只猫来命名。我认为应该不惜一切代价避免使用缩写,除了众所周知/明显的缩写(例如del
,如评论中提到的 - 这甚至是一个语言关键字!),它们可以节省大量的输入。但这并不意味着标识符过于冗长。正如上下文对于理解自然语言中的语句很重要一样,通过引用上下文,标识符通常可以保持更短(并且同样易于理解)。在您的示例中,
project_name
完全没问题 - 该过程已称为delete_project
,因此project_name
显然是指要删除的项目的名称。即使只是name
也可能没问题。无需通过附加_to_be_deleted
再次声明。pr_nm_del
? You might as well let a cat name it. I believe abbreviations should be avoided at all cost, except well-known/obvious ones (likedel
, as mentioned in the comments - that one's even a language keyword!) that save a whole lot of typing.But that doesn't mean overly verbose identifiers. Just as context is important to understand statements in natural languages, identifiers can often be kept much shorter (and just as understandable) by referring to context. In your example,
project_name
is perfectly fine - the procedure is already calleddelete_project
, soproject_name
obviously refers to the name of the project to be deleted. Evenname
alone might be fine. No need to state that again by appending_to_be_deleted
.在您的示例中,您有一个名为delete_project 的函数。想知道如何称呼存储要删除的项目的变量?只是“项目”!
简单的。
变量名称不必是完全描述性的。上下文对于我们如何理解特定时间点的特定名称有很大帮助。在讨论删除项目的功能时,无需说“这是要删除的项目”。
如果您发现函数名称太长,则它们可能做了太多事情。如果您发现变量名称变得太长,请考虑它们在当前上下文中的用途,并查看是否可以隐含名称的一部分。
In your example, you have a function called delete_project. Wondering what to call the variable that stores the project to be deleted? Just 'project'!
Simple.
Variable names don't have to be fully descriptive. Context can lend a lot to how we understand a particular name at a particular point in time. No need to say "this is the project to be deleted" when discussing a function that deletes project.
If you find function names are too long, they're probably doing too much. If you find variable names are becoming too long, think about their purpose in the current context, and see if part of the name can be implied.
当您进行 OOP 时,这个问题会自行解决。主语(
项目、配置
)是类,动词(删除等
)是方法名称,即:This is a problem that kind of solves itself when you're doing OOP. The subject (
project, configuration
) is the class and the verb (delete, etc
) is the method name ie:我认为只要具有描述性,长名称是可以接受的。使用好的编辑器/IDE,短名称无法节省打字时间,而长但具有描述性的名称可以节省阅读代码的时间。所以名字的阅读时间的长度比名字的实际长度重要得多。
至于您的示例,
project_name_to_be_deleted
就可以了。如果你想缩短它,我建议使用project_name_to_del
因为del
是delete
的众所周知的缩写(你甚至可以在你的键盘)。使用conf
作为配置
也很流行。但不要更进一步。例如,proj2del
不是一个好主意。另外,对于内部/本地事物,可以使用简短的、不那么具有描述性的名称。
I think long names are acceptable provided that they are descriptive. With a good editor/IDE, short names cannot save typing, while long but descriptive names can save the time of reading your code. So the length of the reading time of the name is much more important than the actual length of the name.
As for your example,
project_name_to_be_deleted
is fine. If you want to shorten it, I will suggest usingproject_name_to_del
sincedel
is a well-known abbreviation fordelete
(you can even find this in your keyboard). And the useconf
asconfiguration
is also popular. But don't go further. For example,proj2del
is not a good idea.Also, for internal/local things, it's fine to use short-not-so-descriptive names.
很少有名字有“标准”缩写。你认为的“通用”或“标准”对其他人来说并不相同。
花在设置一个好名字上的时间是值得投入的,因为代码被阅读的次数比编写的次数要多得多。
举个例子,我见过
“配置”
缩写为
“配置”
“cnfg”
“cfg”
“cnf”
...所以教训是 - 不要缩写,除非有一个众所周知的缩写!
Very few names have "standard" abbreviations. What you think is "common" or "standard" is not the same for someone else.
The time spent setting a good name is well invested, as code is read much more often than it is written.
As an example, I've seen
"configuration"
abbreviated as
"config"
"cnfg"
"cfg"
"cnf"
...so the lesson is - don't abbreviate unless there is a very well known abbreviation of it!