On Wed, Aug 8, 2012 at 3:59 PM, MichaelMoore
<[email protected]> wrote:
>
>>>>> l = [[]]*3
>>>>> k = [[] for i in range(3)]
>>>>> l==k
> True
>>>>>
>
> The tree produced when using the list comprehension is normal, while the
> tree produced with the multiplication nests deeper and deeper without ever
> returning to the second level.  That code is called inside a user class
> modeled after the treeitem demonstration class TreeDemo(SimplePanel).  If
> someone will tell me where, I will send the code.  Tis a bit long for here,
> but it is a real-world app.on which I am currently working.

im not 100% sure what your question is or if you are requesting
something, but these two methods are not the same. this is more of a
general python issue: you are confusing/mixing `comparable` and
identical:

`==` compare (is A holding the SAME "VALUE" as B?)
`is` identity (is A the EXACT SAME OBJECT as B?)

... other langs may use `===` or something for the identity operator
but the result is the same:

>>> l = [[]]*3
>>> k = [[] for i in range(3)]
>>> l==k
True
>>> map(id, l)
[140651978035928, 140651978035928, 140651978035928]
>>> map(id, k)
[140651978036360, 140651978035568, 140651978036216]
>>> l[0] is l[1] is l[2]
True
>>> k[0] is k[1] is k[2]
False

... `k` creates 4 lists ... one containing the other 3:

list([list(), list(), list()])

... `l` creates 2 lists ... one containing 3 REFERENCES to the other:

ref = list()
list([ref, ref, ref])

... which, depending on context, can be a BIG difference (walking a
tree is one such context ;-)

-- 

C Anthony

-- 



Reply via email to