On 4 Dec, 2005, at 2:09, RaeNye wrote:
Why use this strange name "mlist_a"? Instead use:
multiple_lists = [list1, list2]
I don't see a problem with Guy's name selection. I consider your
suggestion
worse, as the object called "multiple_lists" is, in fact, a single
list that
contains two other lists. You might call the outer list "nested_list"
or
"outer_list", but "multiple_lists" has nothing to do with the object
itself.
"lists" will be a better name. But why the names in this code are so
lousy? because its not a concrete example, its too abstract. Using real
life examples can make everything more clear.
For example, bart simpson downloaded our first game, and lisa created a
list of scores for each family member:
>>> lisa_scores = [90, 85, 80]
>>> bart_scores = [50, 65]
>>> homer_scores = [] # too stupid to play
>>> marge_scores = [] # too busy
# Lets keep all the scores in one list
>>> simpsons_scores[lisa_scores, bart_scores, homer_scores,
marge_scores]
Now we can ask questions about it:
What were lisa scores?
>>> simpsons_scores[0]
[90, 85, 80]
>>> lisa_scores
[90, 85, 80]
they look equal...
>>> simpsons_scores[0] == lisa_scores
True
They are.
Is it the same list of a copy?
>>> simpsons_scores[0] is lisa_scores
True
This answer the question, what happens when you do list = [foo, bar] -
you create reference to the objects.
homer and marge have both empty list. They must be equal:
>>> homer_scores == marge_scores
True
But they are not the same list:
>>> homer_scores is marge_scores
False
lisa teach homer to play, and add some scores:
>>> homer_scores.append(10)
the simpsons_scores list updated:
>>> simpsons_scores
[[90, 85, 80], [50, 65], [10], []]
homer_scores and simpsons_scores[2] reference the same list object
(which Python manage for us somewhere in the memory)
homer tries again, made some improvement:
>>> homer_scores.append(11)
>>> simpsons_scores
[[90, 85, 80], [50, 65], [10, 11], []]
bart is not happy with lisa results:
>>> del lisa_scores
>>> lisa_scores
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'lisa_scores' is not defined
That was evil!
>>> simpsons_scores
[[90, 85, 80], [50, 65], [10, 11], []]
F**k! they are still there!? hmm, lisa_scores was not the list, its
only a name, the list is still there. This is exactly what Python told
us just now: "NameError: name 'lisa_scores' is not defined".
lisa fixes the situation
>>> lisa_scores = simpsons_scores[0]
>>> lisa_scores
[90, 85, 80]
Now [[90, 85, 80], [50, 65], [10, 11], []] is a lousy way to keep this
kind of data, because we have to remember the order of the family
members. This leads naturally to dicts - because we need it NOW to
write simpler and more readable code.
I hope it helps, It was fun to write :-)
Best Regards,
Nir Soffer