deepcopy for object insertion

Youngha Yoon youngha.yoon at gmail.com
Fri Apr 13 22:31:57 EDT 2007


Hello!

I wrote next code.
I intended to insert class object  to Pool list.

<Code>==========================================
import random,copy
class Particle:
    Dimension = 2
    CurrentPosition = {}
    def __init__(self, dimension):
        self.Dimension = dimension
        for i in range(self.Dimension):
            value = random.uniform(0.,1.)
            self.CurrentPosition[i] = value

class Group:
    Pool = []
    def __init__(self, size, dimension):
        for i in range(size):
            particle = Particle(dimension)
            print 'particle generated'
            print particle.CurrentPosition
            self.Pool.append(copy.deepcopy(particle))
            print '\n total Pool'

group = Group(3, 2)

for i in group.Pool:
    print i.CurrentPosition
==============================================


But the result is like follow.
It always synchronized with final insertion. What do you think is the
problem ?


<Results>=======================================
 $ python test3.py
particle generated
{0: 0.70968382903877392, 1: 0.8019162560229226}
particle generated
{0: 0.76791838585848948, 1: 0.91689882120031052}
particle generated
{0: 0.32582853916678456, 1: 0.72166384931732286}

 total Pool
{0: 0.32582853916678456, 1: 0.72166384931732286}
{0: 0.32582853916678456, 1: 0.72166384931732286}
{0: 0.32582853916678456, 1: 0.72166384931732286}
==============================================



Thank you in advance !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070414/0207ad6e/attachment.html>


More information about the Python-list mailing list