Better Python list Naming Other than “list”

2019-04-05 07:51发布

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?

7条回答
混吃等死
2楼-- · 2019-04-05 08:05

I use a naming convention based on descriptive name and type. (I think I learned this from a Jeff Atwood blog post but I can't find it.)

goats_list
for goat in goats_list : 
    goat.bleat()

cow_hash = {}

etc.

Anything more complicated (list_list_hash_list) I make a class.

查看更多
老娘就宠你
3楼-- · 2019-04-05 08:07

Python stands for readability. So basically you should name variables that promote readability. See PEP20.
You should only have a general rule of consistency and should break this consistency in the following situations:

  1. When applying the rule would make the code less readable, even for someone who is used to reading code that follows the rules.

  2. To be consistent with surrounding code that also breaks it (maybe for historic reasons) -- although this is also an opportunity to clean up someone else's mess (in true XP style)

Also, use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.
All this is taken from PEP 8

查看更多
可以哭但决不认输i
4楼-- · 2019-04-05 08:09

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.

查看更多
▲ chillily
5楼-- · 2019-04-05 08:11
goats

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

查看更多
Root(大扎)
6楼-- · 2019-04-05 08:11

Why not just use unsorted? I prefer to have names, which communicate ideas, not data types. There are special cases, where the type of a variable is important. But in most cases, it's obvious from the context - like in your case. Quick sort is obviously working on a list.

查看更多
该账号已被封号
7楼-- · 2019-04-05 08:13

Just use lst, or seq (for sequence)

查看更多
登录 后发表回答