D
Dave Opstad
I fully understand why this happens:
----------------------------
----------------------------
What is curious to me is the implication that there are multiple,
distinct empty list objects (whose ids are not equal). Is this the case
for all mutable objects?
I wonder if it would be useful to have some symbol whose semantics are
"distinct mutable." For the sake of discussion, let's say the symbol \\
means the same as [] (i.e. it's the empty list), but with this "distinct
mutable" semantic. Then I could safely do something like:
to safely initialize b as a list of distinct empty lists. As it is now,
I have to do something like this:
in order to get the semantic I want. It's not any great hardship, of
course, but I have been bitten by the [[]] * 5 case and its cousins on
several occasions, and it might be nice if there were a separate
semantic as described above to make it more clear what's going on.
Just some random thoughts...
Dave
----------------------------
[[42], [42], [42], [42], [42]]a = [[], [], [], [], []]
b = [[]] * 5
a [[], [], [], [], []]
b [[], [], [], [], []]
a == b True
id(a[0]) == id(a[1]) False
id(b[0]) == id(b[1]) True
a[1].append(42)
a [[], [42], [], [], []]
b[1].append(42)
b
----------------------------
What is curious to me is the implication that there are multiple,
distinct empty list objects (whose ids are not equal). Is this the case
for all mutable objects?
I wonder if it would be useful to have some symbol whose semantics are
"distinct mutable." For the sake of discussion, let's say the symbol \\
means the same as [] (i.e. it's the empty list), but with this "distinct
mutable" semantic. Then I could safely do something like:
Falseb = [\\] * 5
b [[], [], [], [], []]
id(b[0]) == id(b[1])
to safely initialize b as a list of distinct empty lists. As it is now,
I have to do something like this:
b = [[] for x in range(5)]
in order to get the semantic I want. It's not any great hardship, of
course, but I have been bitten by the [[]] * 5 case and its cousins on
several occasions, and it might be nice if there were a separate
semantic as described above to make it more clear what's going on.
Just some random thoughts...
Dave