Python 序列命名约定

发布于 2024-07-15 10:44:45 字数 435 浏览 8 评论 0原文

由于 python 中没有显式类型,我希望能够使用命名约定来区分序列和非序列。 我已经用 python 编程有一段时间了,但我仍然没有找到任何逻辑/实用的方法来命名序列。 当然,我浏览了著名的PEP8,并对google了一下,似乎公认的约定是在变量名末尾添加字母“s”。

假设我们有一个“权重值”序列,因此该序列的变量名称应该是weights。 到目前为止,这还好,但在某些情况下,某些单词以“s”结尾,并且恰好是命名不是序列的变量的更合乎逻辑的方式。 或者假设您将权重序列本身存储到序列中。 “s”命名约定将变量命名为“weightss”,这很难看。 我确信序列有更好的命名约定。

您建议采用哪种序列命名约定?

Since there is no explicit typing in python, I want to be able to make the difference between sequences and non-sequences using a naming convention. I have been programming with python for a little while now, and I still haven't found any logical/practical way to name sequences. Of course, I went through the famous PEP8, and made some research on google, and it seems that the accepted convention is to add the letter "s" at the end of the variable name.

Let's assume we have a sequence of "weight values", therefore the variable name for the sequence should be weights. So far that's fine, but there will be cases where some word ends with "s" and happen to be the more logical way to name a variable which is not a sequence. Or let's say you have sequences of weights themselves stored into a sequence. The "s" naming convention would name the variable weightss, which is ugly. I am sure there is be a better naming convention for sequences.

What naming convention for sequences would you advise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小姐丶请自重 2024-07-22 10:44:45

一般来说,要避免这种行为。 来自 PEP8 的通知

愚蠢的一致性是妖精
小心思

作用正是调用变量 weightss 的作用。 因此,一般来说,您的变量描述它们是什么,而不是根据某些命名约定:

weights = [44, 66, 88]
weight_groups = [[44, 66, 88], ...]

等等。

来自 PEP8

但最重要的是:知道什么时候该做
不一致——有时是风格
指南并不适用。 当在
如有疑问,请运用您的最佳判断。 看
在其他例子中并决定什么
看起来最好。 并且不要犹豫
问!

In general, avoid this kind of behaviour. Notice from PEP8

A Foolish Consistency is the Hobgoblin
of Little Minds

which is exactly what calling a variable weightss would be doing. So in general have your variables describing what they are, not according to some naming convention:

weights = [44, 66, 88]
weight_groups = [[44, 66, 88], ...]

etc.

From the same section of the PEP8:

But most importantly: know when to be
inconsistent -- sometimes the style
guide just doesn't apply. When in
doubt, use your best judgment. Look
at other examples and decide what
looks best. And don't hesitate to
ask!

夜空下最亮的亮点 2024-07-22 10:44:45

“s”命名约定将变量weightss命名为,这很难看。 我确信有更好的序列命名约定。

我认为你所描述的约定应该被解释为“每当你有某物的列表时,通过复数形式明确它是一个列表”。 例如,如果您有草实例的列表,则可以将此称为 grasses,而不是 grasss。 我不认为它应该像你所理解的那样从字面上理解。

PEP 始终建议您采用自己的方法(如果这种方法更具可读性和实用性)。 正如阿里提到的,指导原则之一PEP 的核心是你不应该成为愚蠢的一致性的牺牲品。

The "s" naming convention would name the variable weightss, which is ugly. I am sure there is be a better naming convention for sequences.

I think the convention you're describing is meant to be interpreted as "whenever you have list of something, make it clear that it's a list by pluralizing it". For example, if you have a list of instances of grass, you would call this grasses, not grasss. I don't think it's meant to be taken as literally as you're taking it.

PEP always advises you to take your own approach if that is more readable and useful. As Ali mentioned, one of the guiding principles of PEP is that you shouldn't fall prey to foolish consistencies.

原谅我要高飞 2024-07-22 10:44:45

无论你的小心脏想要什么......

只是开玩笑,但我不会挂断电话。 如果它很难看,请采取一些措施使其更具可读性,例如 seq_weightseq_weights

Whatever you little heart desires....

Just kidding, but I wouldn't get to hung up on it. If it's ugly, do something to make it more readable like seq_weight and seq_weights

萝莉病 2024-07-22 10:44:45

为什么不只是 thing_listthing_seq

Why not just thing_list or thing_seq?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文