copying builtin types (WAS: list IndexError)
Steven Bethard
steven.bethard at gmail.com
Thu Dec 23 12:05:15 EST 2004
Grant Edwards wrote:
> I would have guessed that calling list() on a list
> was a noop. I would be wrong. Surprised, but wrong.
I guess it's probably worth pointing out that most builtin mutable types
can be copied using the type constructor:
py> def check(obj):
... copy = type(obj)(obj)
... print id(obj), id(copy)
... return copy
...
py> check([1, 2])
18124312 18124752
[1, 2]
py> check({1:2})
18102720 18126720
{1: 2}
py> check(set([1, 2]))
9675672 9675504
set([1, 2])
For immutable types, this is indeed basically a noop:
py> check(12)
3303412 3303412
12
py> check('12')
18120128 18120128
'12'
py> check((1, 2))
18122552 18122552
(1, 2)
Steve
More information about the Python-list
mailing list