views:

417

answers:

4

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?

A: 

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

jcoon
A: 

Why not just thing_list or thing_seq?

slf
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
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
What's wrong with thing_seq as a generalization of thing_list?
S.Lott
I like thing_seq just fine too
slf
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
+17  A: 

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!

Ali A
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 :)
J.F. Sebastian
I've added another quote from the PEP8. Feel free to rollback
J.F. Sebastian
+9  A: 

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.

John Feminella