我们应该使用哪些缩写来表示python变量名? [英] Which abbreviations should we use for python variable names?

查看:407
本文介绍了我们应该使用哪些缩写来表示python变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我使用PEP-8中的标准命名来表示变量。像:

  delete_projects 
connect_server

然而,有时候我找不到任何好名字,而且名字只是延长了一个长长的名字:

  project_name_to_be_deleted 

我可以使用 pr_nm_del ,但是这使得代码不可读。我真的很难找到好的函数变量名称。每当我开始写一个新的函数,我只是花时间去找一个好的变量名。

是否有任何标准为已知的变量名称选择某些缩写,例如 delete,project,configuration等?你如何选择简短,但好的和可读的变量名称?



这个问题可能不是直接依赖于Python,但是由于不同的编程语言使用不同的变量名称格式,所以我想我只限制这个问题 >。

解决方案

pr_nm_del ?你可以让一只猫命名它。我相信应该不惜一切代价来避免缩写,除了那些知名/明显的缩写(如评论中提到的 del ,甚至是一个语言关键字!一大堆的打字。



但是,这并不意味着过于详细的标识符。正如上下文对理解自然语言中的语句很重要一样,通过引用上下文,标识符通常可以保持更短(并且可以理解)。在你的例子中, project_name 完全没问题 - 这个过程已经被称为 delete_project ,所以 project_name 显然是指要删除的项目名称。即使是 name 也可以。不需要再次声明 _to_be_deleted


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.

解决方案

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 (like del, 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 called delete_project, so project_name obviously refers to the name of the project to be deleted. Even name alone might be fine. No need to state that again by appending _to_be_deleted.

这篇关于我们应该使用哪些缩写来表示python变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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