0

我有以下代码

    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

对象放置包含一个国家列表,这些国家本身包含一个广告素材列表。因此,当我运行此代码时,我注意到每个展示位置具有完全相同数量的国家/地区对象,包含在列表对象placement.countries 中。其实这不可能。我想我在代码中做错了,但我不知道是什么。

这是对象代码

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  
4

1 回答 1

0

使内部变量基于实例而不是基于类....

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  
于 2012-09-03T20:36:51.760 回答