Python序列命名约定 [英] Python sequence naming convention

查看:119
本文介绍了Python序列命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于python中没有显式输入,因此我希望能够使用命名约定来区分序列和非序列.我已经用python编程了一段时间了,但我仍然没有找到任何逻辑/实用的方式来命名序列.当然,我经历了著名的 PEP8 ,并对google,并且似乎可以接受的约定是在变量名称的末尾添加字母"s".

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.

假设我们有一个权重值"序列,因此序列的变量名称应为 weights .到目前为止,这还不错,但是在某些情况下,某些单词以"s"结尾,并且碰巧是一种更合理的方式来命名不是序列的变量.或假设您将权重序列本身存储在序列中. "s"命名约定会将变量 weightss 命名为丑陋的.我确信序列会有更好的命名约定.

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?

推荐答案

通常,避免这种行为.请注意 PEP8

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

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

A Foolish Consistency is the Hobgoblin of Little Minds

恰恰是调用变量weightss会执行的操作.因此,通常不按照某些命名约定让变量描述它们的含义:

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], ...]

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!

这篇关于Python序列命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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