I'm trying to clarify my understanding regarding situations in the code in which the singular seems to extract from the plural. I'm not sure if this is standard among different languages, or, unique to specific situations in python. Regardless, in the example below I refer to square within the squares list:
squares=['white', 'black', 'red', 'blue', 'green']
for i, square in enumerate(squares):
print(i, square)
0 white
1 black
2 red
3 blue
4 green
Is python intuitive enough to know I am referring to the singular square from the "squares" list? Is it unique to just this type of coding request or is it more broad and standard? etc.
The variables square
and squares
are completely unrelated. In this case, the use of the variable square
(singular) is an arbitrary decision by the coder. I.e.
squares=['white', 'black', 'red', 'blue', 'green']
for i, square in enumerate(squares): print(i, square)
...would work identically had the coder chosen ...
wombats=['white', 'black', 'red', 'blue', 'green']
for oodles, noodles in enumerate(wombats): print(oodles, noodles)
The enumerate
method is a generator which iterates through a list, and yields each item in the list's index, and value in that order. By convention only, coders often choose i
to stand for index
and will frequently give the list a plural name, and the variable intended to hold the value the singular of that plural.
But it is entirely arbitrary and up to the coder's preferences what the variable's names are.