简体   繁体   English

Python 序列命名约定

[英]Python sequence naming convention

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.由于 python 中没有明确的类型,我希望能够使用命名约定来区分序列和非序列。 I have been programming with python for a little while now, and I still haven't found any logical/practical way to name sequences.我已经用 python 编程了一段时间,但我仍然没有找到任何逻辑/实用的方式来命名序列。 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.当然,我浏览了著名的PEP8 ,并在 google 上做了一些研究,似乎接受的约定是在变量名的末尾添加字母“s”。

Let's assume we have a sequence of "weight values", therefore the variable name for the sequence should be weights .假设我们有一个“权重值”序列,因此序列的变量名应该是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.到目前为止,这很好,但是在某些情况下,某些单词以“s”结尾并且恰好是命名不是序列的变量的更合乎逻辑的方式。 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. “s”命名约定将命名变量weightss ,这很难看。 I am sure there is be a better naming convention for sequences.我确信序列有更好的命名约定。

What naming convention for sequences would you advise?你会建议什么样的序列命名约定?

In general, avoid this kind of behaviour.一般来说,避免这种行为。 Notice from PEP8来自PEP8的通知

A Foolish Consistency is the Hobgoblin of Little Minds愚蠢的一致性是小精灵的妖精

which is exactly what calling a variable weightss would be doing.这正是调用可变weightss重所做的事情。 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 :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!不要犹豫,问!

The "s" naming convention would name the variable weightss, which is ugly. “s”命名约定将命名变量权重,这很难看。 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 .例如,如果您有一个草实例列表,您可以将此称为grasses ,而不是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. PEP 总是建议您采用自己的方法,如果这种方法更具可读性和实用性的话。 As Ali mentioned , one of the guiding principles of PEP is that you shouldn't fall prey to foolish consistencies.正如阿里所提到的,PEP 的指导原则之一是你不应该成为愚蠢一致性的牺牲品。

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如果它很难看,请做一些事情以使其更具可读性,例如seq_weightseq_weights

Why not just thing_list or thing_seq ?为什么不只是thing_listthing_seq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM