I don't understand why this happens:
When I use the add_promo()
function to add a promo
object directly to self.spl
(using its self.spl.add(promo)
function) each Day object gets a different StoryPromoList
object.
But if I add the promo
through self.cpl.add_promo(promo)
which basically just does the same thing but with an extra step, each Day object gets assigned the same StoryPromoList
object, instead of different objects.
The StoryPromoList
objects are created but only the last one is used for all days for some reason.
This is the Day
class:
class Day():
def __init__(self, date:date):
self.date = date
self.spl = StoryPromoList()
self.fpl = FeedPromoList()
self.cpl = CombinedPromoList(self.spl, self.fpl)
self.weekday = self.date.weekday()
def __repr__(self):
return f"<Day object (date='{self.date}')>"
def add_promo(self, promo):
self.cpl.add_promo(promo)
# assign promo to the corresponding timeblock
def get_all_promos(self):
print(self.spl._id, self.date, self.cpl)
return {'story_promos': [p for p in self.cpl.spl.list], 'feed_promos': [p for p in self.cpl.fpl.list]}
When I execute the get_all_promos()
function for each day I get this output:
6 2021-09-27 <__main__.CombinedPromoList object at 0x00000244AADABBE0>
6 2021-09-28 <__main__.CombinedPromoList object at 0x00000244AADAB9A0>
6 2021-09-29 <__main__.CombinedPromoList object at 0x00000244AADAB730>
6 2021-09-30 <__main__.CombinedPromoList object at 0x00000244AADAB4C0>
6 2021-10-01 <__main__.CombinedPromoList object at 0x00000244AADAB250>
This is the CombinedPromoList
class:
class CombinedPromoList():
def __init__(self, spl=StoryPromoList(), fpl=FeedPromoList()) -> None:
self.spl = spl
self.fpl = fpl
def add_promo(self, promo):
if promo.type_name() == 'story':
self.spl.add(promo)
elif promo.type_name() == 'feed':
self.fpl.add(promo)
And this is the StoryPromoList
which is the same as FeedPromoList
with minor differences:
class StoryPromoList():
classes = []
_id = 0
def __init__(self, list=[]):
self.id=StoryPromoList._id
StoryPromoList._id += 1
StoryPromoList.classes.append(self)
self.list = list
def add(self, storypromo:StoryPromo):
self.list.append(storypromo)