2

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?

Mihai Limbășan
  • 64,368
  • 4
  • 48
  • 59
Manu
  • 191
  • 1
  • 8

4 Answers4

20

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!

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
  • The paragraph that starts with the above quote ends with "To be great is to be misunderstood." http://www.emersoncentral.com/selfreliance.htm Therefore great answers on SO are doomed to be downvoted :) – jfs Mar 18 '09 at 18:42
  • I've added another quote from the PEP8. Feel free to rollback – jfs Mar 18 '09 at 18:49
10

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.

Community
  • 1
  • 1
John Feminella
  • 303,634
  • 46
  • 339
  • 357
0

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

Jason Coon
  • 17,601
  • 10
  • 42
  • 50
0

Why not just thing_list or thing_seq?

slf
  • 22,595
  • 11
  • 77
  • 101
  • My biggest reason would be that it's a leaky abstraction of a name (list is only one kind of sequence). Otherwise not too bad, though. weights plural of weights, weights_list or weights_group or whatever as plural of weights. – Devin Jeanpierre Mar 18 '09 at 18:03
  • That's what I begun with, but then I went back to the "s" convention. The problem is that if you decide to switch from a list implementation to a dictionary implementation, the "_list" suffix will no longer match what's inside. – Manu Mar 18 '09 at 18:03
  • What's wrong with thing_seq as a generalization of thing_list? – S.Lott Mar 18 '09 at 19:09
  • I like thing_seq just fine too – slf Mar 19 '09 at 22:57
  • But with this convention, a sequence of sequences would have to be named thing_seq_seq. One could just name it thing_seq, but then there would be no difference between sequences and sequences of sequences, and the problem would be the same than with the "plural convention" discussed above. – Manu Mar 20 '09 at 15:24