我有以下代码
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
答案 0 :(得分: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