-3

I'm trying to write A dataclass for my python program , My idea was to use the dataclass as the full database , But I do not know how to access the variable from a dataclass.

I'd be glad if Someone would be able to point me in the right direction.

@dataclass
class database:
    """The whole program database that holds all the variables"""

    def birth_database():

        birthdate_add_pass : str = None
        birth_day : int = None
        birth_month : int = None
        birth_year : int = None
        full_birth_date : str = f"{birth_day}/{birth_month}/{birth_year}"

        confirm_birthday : str = None
        confirm_birthmonth : str = None
        confirm_birthyear : str = None
        confirm_full_birthdate : str = None


user_birthdate = database.birth_database.birth_day
deceze
  • 510,633
  • 85
  • 743
  • 889
KhodeNima
  • 1
  • 3
  • 2
    Your "database" has no data. It only has empty local variables. – luk2302 Aug 10 '23 at 06:11
  • 1
    That’s just not how that works at all. – deceze Aug 10 '23 at 06:14
  • @luk2302 Pardon me for the confusion , But I always assume that It's a great thing to define all the variable name's before even starting to write the main code , And that kind of structure always helped me a lot , Matter of fact , In the current project that I used this dataclass , I've had to use FLAG variables for my loops , And since the project had ton's of nested loop's , It would've been a torture to give them the value of False again , So simply with the usage of dir() method , And __contains__ dunder method , I gained control over my variables , Hope that explains the use case. – KhodeNima Aug 20 '23 at 04:39

2 Answers2

0

it looks like you are confusing the concept class and method. You might want to have a look into that.

Answering your question: You are declaring your variables inside your instance method (which won't work by the way because it's inside a class without a self) and not under the scope of the data class, this means that these variables would only exist 'inside this method'.

There are quite a lot of errors here and there, but probably what you want looks something like this:

from dataclasses import dataclass


@dataclass
class BirthDatabase:
    birthdate_add_pass: str = None
    birth_day: int = None
    birth_month: int = None
    birth_year: int = None

    confirm_birthday: str = None
    confirm_birthmonth: str = None
    confirm_birthyear: str = None
    confirm_full_birthdate: str = None

    def __post_init__(self):
        self.full_birth_date: str = f"{self.birth_day}/{self.birth_month}/{self.birth_year}"


user_birthdata = BirthDatabase(birth_day=10, birth_month=8, birth_year=2023)

print(user_birthdata.birth_day)  # prints: 10
print(user_birthdata.full_birth_date)

Anyway, this is probably not a good approach to solving your problem and other solutions might fit better your objective.

Hope this help, and if you are not using any IDE I recommend you use pycharm. Further reading on classes: https://docs.python.org/3/tutorial/classes.html https://realpython.com/python-classes/

For further info on data classes: https://docs.python.org/3/library/dataclasses.html

-1
class database:
"""The whole program database that holds all the variables"""

    class birth_database:
        def __init__(self):
            self.birthdate_add_pass : str = None
            self.birth_day : int = None
            self.birth_month : int = None
            self.birth_year : int = None
            self.full_birth_date : str = f"{self.birth_day}/{self.birth_month}/{self.birth_year}"

            self.confirm_birthday : str = None
            self.confirm_birthmonth : str = None
            self.confirm_birthyear : str = None
            self.confirm_full_birthdate : str = None


user_birthdate = database.birth_database().birth_day