Newbie question: Allocation vs references

Steven Bethard steven.bethard at gmail.com
Thu Jun 2 12:31:31 EDT 2005


Stian Søiland wrote:
> There are several ways to create a copy of a list:
[snip]
> a2 = list(a)       # create a new list object out of any sequence

I'll just point out that FWIW, this is by far my favorite idiom of the 
ones offered because it applies to pretty much all the builtin container 
types:

py> d = {1:42, -37:0}
py> d2 = dict(d)
py> d == d2, d is d2
(True, False)
py> s = set(['a', 'd', 'e'])
py> s2 = set(s)
py> s == s2, s is s2
(True, False)
py> L = [3.14, 0.707]
py> L2 = list(L)
py> L == L2, L is L2
(True, False)

STeVe



More information about the Python-list mailing list