0

I've got the following code

    old_county_code = -1
    old_placement_id = -1
    placements = []
    for row in raw_data: #raw_data is one line from the database retrieved by cursor.fetchall()

        placement_id = row[1]
        if placement_id != old_placement_id:
            placement = Objects.placement()
            placement.placement_id = placement_id
            placements.append( placement )

        country_code = row[3]
        if old_county_code != country_code:
            country = Objects.country()
            country.country_id = country_code 
            placement.countries.append( country )                

        creative = Objects.creative( row[2], row[0], row[4], row[5], row[6], row[7] )
        country.creatives.append( creative )

        old_placement_id = placement_id
        old_county_code = country_code

The object placement contains a list of countries which themselves contain a list of creatives. So, when I run this code I notice that each placement has the exact same number of country object contained in the list object placement.countries. Actually this cannot be the case. I think I did something wrong in my code but I don't know what.

This is the objects code

class placement(object):
    placement_id = 0
    countries = []

class country(object):
    country_id = 0
    creatives = []

class creative(object):
    creative_id = 0
    matching_id = 0
    clicks = 0
    impressions = 0
    ctr = 0.0
    rank = 0.0  
toom
  • 12,864
  • 27
  • 89
  • 128
  • 3
    not sure what your trying to do from your post... make a small example (very small) and show all input and all output then put your expected output – Joran Beasley Sep 03 '12 at 20:27
  • 1
    See also [this question](http://stackoverflow.com/questions/8860447/why-does-python-seem-to-treat-instance-variables-as-shared-between-objects). This is a "class-vs-instance" variable issue. – DSM Sep 03 '12 at 20:27

1 Answers1

0

make the internal variables instance based rather than class based ....

class placement(object):
  def __init__(self):
    self.placement_id = 0
    self.countries = []

class country(object):
  def __init__(self):
    self.country_id = 0
    self.creatives = []

class creative(object):
  def __init__(self)
    self.creative_id = 0
    self.matching_id = 0
    self.clicks = 0
    self.impressions = 0
    self.ctr = 0.0
    self.rank = 0.0  
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179