Python 序列命名约定
由于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,要避免这种行为。 来自 PEP8 的通知
作用正是调用变量
weightss
的作用。 因此,一般来说,您的变量描述它们是什么,而不是根据某些命名约定:等等。
来自 PEP8:
In general, avoid this kind of behaviour. Notice from PEP8
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:etc.
From the same section of the PEP8:
我认为你所描述的约定应该被解释为“每当你有某物的列表时,通过复数形式明确它是一个列表”。 例如,如果您有草实例的列表,则可以将此称为
grasses
,而不是grasss
。 我不认为它应该像你所理解的那样从字面上理解。PEP 始终建议您采用自己的方法(如果这种方法更具可读性和实用性)。 正如阿里提到的,指导原则之一PEP 的核心是你不应该成为愚蠢的一致性的牺牲品。
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
, notgrasss
. 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.
无论你的小心脏想要什么......
只是开玩笑,但我不会挂断电话。 如果它很难看,请采取一些措施使其更具可读性,例如
seq_weight
和seq_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
andseq_weights
为什么不只是
thing_list
或thing_seq
?Why not just
thing_list
orthing_seq
?