通过描述性命名提高 Python 可读性
我使用 Python 已有大约一年的时间了,主要来自于 Java 背景。我发现 Python 非常容易学习,因为它注重可读性和简单的设计。我对 python 不明白的是,为什么对于一种如此注重可读性的语言,它经常对模块、函数、常量等使用非常非描述性的名称。我喜欢 Java 的一件事是它非常具有描述性的类/属性/方法名称(因此我更喜欢 Objective-C)。一般来说,Python 程序员似乎都采用了 C 类型的命名方法,他们对所有内容都使用尽可能短的名称。我知道每个人都希望尽可能少地打字,但我喜欢很多程序员将大部分时间花在阅读代码而不是编写代码上,所以我发现在短的非描述性名称和长的描述性名称之间进行选择,这是一个很容易选择的选择。制作。 (我喜欢更长的描述性名称xD)
几个例子,看看标准库中的一些模块,
- sched - 事件调度程序,这可能是EventScheduler吗?
- asyncore — 异步套接字处理程序,AsynchronousSocketHandler?
- imghdr — 确定图像的类型,DetermineImageType?
- 泡菜?
我知道这不是一个大问题,但我发现自己经常不需要查找我遇到的任何新的(或忘记的)模块的含义,而在其他语言(如 Objective-C 或 Java)中我可以直接理解这个含义远离模块/功能/属性定义。 另一方面,人们倾向于编写与标准库类似的代码,因此您可以确定,如果标准库使用非描述性名称,那么普通开发人员将使用更多非描述性名称。
我只是想知道有人知道这是为什么吗?
I have been using Python for about a year now, coming from a mostly Java background. I found Python quite easy to learn because of its focus on readability and simple design. The thing I don't understand about python is why for a language that focuses so heavily on readability, it often uses very non-descriptive names for modules, functions, constants etc.. One thing I like about Java is its very descriptive class/attribute/method names (I like objective-C even more for this reason). It seems python programmers in general seem to have taken a C type approach to naming where they use as short names as possible for everything. I know everyone wants to do as little typing as possible but I like a lot of programmers spend the majority of my time reading code rather that writing it so I find the choice between short non-descriptive names and long descriptive names, an easy one to make. (I like longer descriptive names xD)
A few examples, just looking at some modules in the standard library,
- sched — Event scheduler, Could this have been EventScheduler?
- asyncore — Asynchronous socket handler, AsynchronousSocketHandler?
- imghdr — Determine the type of an image, DetermineImageType?
- Pickle?
I know this isn't a huge issue but I find myself more often than not having to look up the meaning of any new (or forgotten) module I come across when in other languages like Objective-C or Java I can get this meaning straight away from the modules/functions/attributes definition.
On another note, people tend to write code similar to the way the standard library is written so you can be sure that if the standard library uses non-descriptive names the average developer will use even more non-descriptive names.
I was just wondering does anyone know why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想在描述性和简洁性之间需要取得平衡,Python 的脚本背景使其比 Java 更简洁(因为我们这些业余爱好者太懒了,不愿意打字 ;-) )。冒着听起来像狂热分子的风险,Python 试图在描述性但长的(Java)和短但棘手的(*咳嗽*Perl)之间游走。
经常使用且易于理解的东西可能很短,因此我们有一个
str
类型,而不是AsciiString
或UnicodeString
(分别是 Python2/3 )。更专业的函数,例如 urllib.urlencode 或 random.normalvariate ,名称更长。核心语言通常保持简单(例如,没有字符类型,只有单字符字符串)。只有“一种正确的方法”的想法以及鸭子类型意味着不需要像
do_something_with_type_a
这样的名称。而且,虽然这只是一个借口,但对于任何不明显的内容都有明确的文档。至于itertools?模块名称并不重要,它只是一组函数。有些功能更清晰(
chain
、cycle
、repeat
),有些则不太清晰(islice
、>izip
)。我想我们假设一旦您熟悉了 Python,“压缩”和“切片”等概念就很简单了。sched/asyncore/imghdr:诚然,所有这些都简短且缺乏描述性,但我从未见过它们中的任何一个被使用。它们可能可以追溯到 8 字符文件名的时代,更新它们从来都不是优先事项。
pickle:很奇怪,但你只需要查一次,就很明显了。您不能真正将其称为“序列化器”,因为它用于特定的序列化,而不是通用框架。
I guess there's a balance to be struck between being descriptive and concise, and Python's scripting background makes it somewhat more concise than Java (because us hobbyists are too lazy for all that typing ;-) ). At the risk of sounding like a fanboy, Python tries to walk the line between the descriptive-but-long (Java), and the short-but-tricky (*cough*Perl).
Things that are used often and easily understood can be short, so we have a
str
type rather than anAsciiString
orUnicodeString
(Python2/3 respectively). More specialised functions, likeurllib.urlencode
orrandom.normalvariate
get longer names.The core language is generally kept simple (e.g. there is no character type, only one-character strings). The idea that there's only "one right way to do it", along with duck typing, mean that there's no need for names like
do_something_with_type_a
. And, while it's just an excuse, there is clear documentation for anything that's not obvious.As for itertools? The module name doesn't really matter, it's just a grouping of functions. Some of the functions are clearer (
chain
,cycle
,repeat
), some less so (islice
,izip
). I suppose we assume that concepts like "zipping" and "slicing" are straightforward once you're familiar with Python.sched/asyncore/imghdr: All admittedly brief and undescriptive, but I've never seen any of them used. They probably date back to the days of 8-character filenames, and updating them has never been a priority.
pickle: Quirky, but you really only have to look it up once, then it's obvious. You couldn't really call it "serializer", because it's for a specific serialisation, not a generic framework.
看看 PEP 8
亮点:
然后是Python之禅。无论如何,据我所知,Python 社区没有规定使用明确的、描述性的、明显的名称——这由开发人员决定。
Take a look at PEP 8
Highlights:
And then there's the Zen of Python. In any case, AFAIK there is no prescription for using explicit, descriptive, obvious names driven by the Python community - it's left up to the developer.