Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better Python list Naming Other than "list"

Is it better not to name list variables "list"? Since it's conflicted with the python reserved keyword. Then, what's the better naming? "input_list" sounds kinda awkward.

I know it can be problem-specific, but, say I have a quick sort function, then quick_sort(unsorted_list) is still kinda lengthy, since list passed to sorting function is clearly unsorted by context.

Any idea?

like image 618
clwen Avatar asked Oct 16 '11 14:10

clwen


2 Answers

I like to name it with the plural of whatever's in it. So, for example, if I have a list of names, I call it names, and then I can write:

for name in names:

which I think looks pretty nice. But generally for your own sanity you should name your variables so that you can know what they are just from the name. This convention has the added benefit of being type-agnostic, just like Python itself, because names can be any iterable object such as a tuple, a dict, or your very own custom (iterable) object. You can use for name in names on any of those, and if you had a tuple called names_list that would just be weird.

(Added from a comment below:) There are a few situations where you don't have to do this. Using a canonical variable like i to index a short loop is OK because i is usually used that way. If your variable is used on more than one page worth of code, so that you can't see its entire lifetime at once, you should give it a sensible name.

like image 116
andronikus Avatar answered Nov 10 '22 20:11

andronikus


goats

Variable names should refer what they are not just what type they are.

like image 6
Jakob Bowyer Avatar answered Nov 10 '22 18:11

Jakob Bowyer