Python Forum

Full Version: Need to fix my brain on classes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to do a simple calendar to learn practical use of classes, because I find classes counterintuitive in python and I struggle a lot with it.

my code rn looks like this:
from dataclasses import dataclass
from rich import print


class Date(object):
    def __init__(self, year:int, month:int, day:int):
        self.year = self.Year(year)
        self.month = self.Month(month)
        self.day = self.Day(day)
    
    class Month:
        def __init__(self, name:str):
            assert name.capitalize() in self.Months.names, f'''[ERROR] : No such month {name}
                Possible names are : {self.Months.names}
            '''
            self.id = self.Months.names.index(name)
            self.days = self.Months.days[id]
            self.name = self.Months.names[id]
        def __repr__(self):
            return self.id, self.name, self.days

    class Year:
        def __init__(self, year:int):
            self.year = year
            self.leap = not year % 4
        def __repr__(self):
            return self.year, self.leap

    @dataclass
    class Day:
        id : int

    @dataclass
    class Months:
        names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    def __repr__(self):
        return f'{self.day}:{self.year}:{self.month}'
I need someone to review this, and give me some examples on what here sucks and how it should be done properly, I think this multiple nesting is one of the issues here, but I wanted to be a single-class calendar.
PS.it is not even working
In [87]: Date(1994,'July', 1)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [87], in <cell line: 1>()
----> 1 Date(1994,'July', 1)

Input In [86], in Date.__init__(self, year, month, day)
      2 def __init__(self, year:int, month:int, day:int):
      3     self.year = self.Year(year)
----> 4     self.month = self.Month(month)
      5     self.day = self.Day(day)

Input In [86], in Date.Month.__init__(self, name)
      8 def __init__(self, name:str):
----> 9     assert name.capitalize() in self.Months.names, f'''[ERROR] : No such month {name}
     10         Possible names are : {self.Months.names}
     11     '''
     12     self.id = self.Months.names.index(name)
     13     self.days = self.Months.days[id]

AttributeError: 'Month' object has no attribute 'Months'

In [88]: Date(1994,'July', 1)
where's the rest of the code? imports, example usage, etc.
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]