Better Python List Naming Other than a "List",

Isn't it better to list list variables? Since this contradicts the python reserved keyword. Then what is better to call? "input_list" sounds awkward.

I know this may be a problem, but let's say I have a quick sort function, then quick_sort (unsorted_list) is still quite long, since the list passed to the sort function is clearly not sorted by context.

Any idea?

+8
python naming-conventions
source share
7 answers

I like to call it the plural of what'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 pretty. But in general, for your own sanity, you must specify your variables so that you can know that they are on behalf of. This convention has the added benefit of being a type agnostic, just like Python itself, because names can be any iterable object, such as a tuple, dict, or your own (iterable) object. You can use for name in names for any of them, and if you have a tuple called names_list , that would just be weird.

(Added from the comment below :) There are several situations where you do not need to do this. Using a canonical variable of type i to index a short loop is fine, because i usually used this way. If your variable is used on more than one page with code, so you cannot immediately see it all your life, you should give it a reasonable name.

+17
source share
 goats 

Variable names should refer to the fact that they are not only what they are.

+6
source share

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

  • When applying a rule, the code will be less readable, even for someone who is used to reading code that follows the rules.

  • To match the surrounding code, which also violates it (perhaps for historical reasons) - although this is also an opportunity to remove someone else's mess (in true XP style)

In addition, use the rules for naming functions: lowercase letters with words underlined with underscores to increase readability.
All of this is taken from PEP 8.

+5
source share

Why not just use unsorted ? I prefer to have names that communicate ideas, rather than data types. There are special cases where the type of a variable is important. But in most cases, this is obvious from the context - as in your case. Quicksort obviously works on a list.

+1
source share

How about L ?

+1
source share

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

 goats_list for goat in goats_list : goat.bleat() cow_hash = {} 

and etc.

Something more complicated (list_list_hash_list) I am creating a class.

+1
source share

Just use lst or seq (for consistency)

0
source share

All Articles