0

I have this class when never i execute it it give me this error I know the error in day_name = self.day the day is being read as is it instead of variable

Traceback (most recent call last): File "c:\Users\LENOVO\Documents\python\usefull\test.py", line 19, in day_is_1 = ob.get_day(day_1) File "c:\Users\LENOVO\Documents\python\usefull\test.py", line 13, in get_day day_name = self.day AttributeError: 'Days' object has no attribute 'day'

class Days:

    def __init__(self):
        self.mo = "monday"
        self.tu = "tuesday"
        self.we =  "wednesday"
        self.th = "thursday"
        self.fr = "friday"
        self.sa = "saturday"
        self.su =  "sunday"
    
    def get_day(self,day):
        day_name =  self.day
        print(day_name)

day_1 =  "mo"
day_2 = "tu"
ob =  Days()
day_is_1 =  ob.get_day("mo")
day_is_2 =  ob.get_day("tu")
urek mazino
  • 130
  • 12
  • 1
    The dupe might seem a bit vague (since it talks about functions+modules instead of attributes+objects) but the technique used is the same: use `day_name = getattr(self, day)`. – wim Mar 16 '22 at 23:45
  • works like charm tnx ❤️ that's saved me from a lot of if statements @wim – urek mazino Mar 16 '22 at 23:52

0 Answers0