+ 1

Im trynig to print a list using the repr method but im getting an error

Here's my code: class Appointment: printlist = [] def __init__(self,day,month,year,desc): self.day = day self.month = month self.year = year self.desc = desc def OccursOn(self,year,month,day): pass def __repr__(self): return self.printlist class OneTime(Appointment): def OccursOn(self,year,month,day): if (self.day == day and self.month == month and self.year == year): super().printlist.append(self) def __repr__(self): return Appointment.__repr__(self) class Daily(Appointment): def OccursOn(self,year,month,day): super().printlist.append(self) def __repr__(self): return Appointment.__repr__(self) class Monthly(Appointment): def OccursOn(self,year,month,day): if self.day == day: super().printlist.append(self) def __repr__(self): return Appointment.__repr__(self) #=================================================================================== p1 = OneTime(12,12,12, 'fracture') p2 = Monthly(12,10,14, 'head ache') pat = [p1,p2] for i in pat: i.OccursOn(12,12,12) print(Appointment.printlist)

3rd Jun 2020, 5:04 AM
Yash
Yash - avatar
1 Resposta
+ 1
As I can see, you defined the ``printlist´´ variable outside of the __init__ function. So you cannot use it with ``self´´.
3rd Jun 2020, 10:05 PM
ErtyumPX
ErtyumPX - avatar