Pointers/References in Python?

Maric Michaud maric at aristote.info
Wed Jul 30 17:20:05 CEST 2008


Le Wednesday 30 July 2008 16:46:37 boblatest at googlemail.com, vous avez écrit :
> Hello,
>
> I have a long list of memory-heavy objects that I would like to access
> in differently sorted order. Let's say I'd like to have lists called
> by_date or by_size that I can use to access the objects in the
> specified order.
>
...
> I know there is a thing called "shallow copy" that has something to do
> with not duplicating memory content but I don't understand the
> concept. Maybe that's what would help here, too.
>

In fact, all containers in python contains references of objects.

>>>[54]: a = [1]

>>>[55]: b = [a]

>>>[56]: c = list(sorted(b))

>>>[57]: b, c
...[57]: ([[1]], [[1]])

>>>[58]: b.append(3)

>>>[59]: b, c
...[59]: ([[1], 3], [[1]])

>>>[60]: a.append(0)

>>>[61]: b, c
...[61]: ([[1, 0], 3], [[1, 0]])

This is if you want to make a true copy (called deep copy) that you'll have to 
do extra steps (using copy module for example).

-- 
_____________

Maric Michaud



More information about the Python-list mailing list