Python Forum
Need to fix my brain on classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to fix my brain on classes
#3
I guess you are doing something like:
birthday = Date(1994, "July", 1)
First, in your __init__() you say Date() expects month as an int. But you are passing a string. Right? So it should be:
(Apr-29-2022, 03:36 PM)blackknite Wrote:
class Date(object):    def __init__(self, year:int, month:str, day:int):

(Apr-29-2022, 03:36 PM)blackknite Wrote:
----> 9     assert name.capitalize() in self.Months.names, f'''[ERROR] : No such month {name}
...
Error:
AttributeError: 'Month' object has no attribute 'Months'
This is because Months is a subclass of Date. In subclass Month you are referring to "self.Months.names" but "self" would mean it is inside the subclass Month which is not the case. So you must refer to "Date.Months.names".

Then at last you have:
            self.id = self.Months.names.index(name)
            self.days = self.Months.days[id]
            self.name = self.Months.names[id]
Here you assign a value to "self.id", but then you are referring to it as "id". Be consequent; it should be:
            self.id = Date.Months.names.index(name)
            self.days = Date.Months.days[self.id]
            self.name = Date.Months.names[self.id]
Reply


Messages In This Thread
Need to fix my brain on classes - by blackknite - Apr-29-2022, 03:36 PM
RE: Need to fix my brain on classes - by Larz60+ - Apr-29-2022, 10:32 PM
RE: Need to fix my brain on classes - by ibreeden - May-01-2022, 12:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Old brain - New Language - Question on loops sailingbikeruk 6 5,527 Oct-03-2018, 08:27 PM
Last Post: nilamo
  Using classes? Can I just use classes to structure code? muteboy 5 6,529 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020