Python Using Databases in Python Our Diary App Doing Data Entry

Robust Tran
Robust Tran
2,802 Points

The purpose of put a class inside another class?

In Python why we put a class inside another class like Meta class inside Diary class? I've tried to figure it out but still don't get it!

Kenneth Love. Can you help me out, thanks :)

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It's just an organizational thing. You can have classes inside of classes and functions inside of functions, etc. This way you have a class that belongs to a parent class and you can instantiate that class when you create an instance of the parent class, then the attributes for that Meta class belong to it, and are encapsulate in it, instead of cluttering up your parent class.

You can also provide defaults in that Meta class based on the class that your parent class inherits from.

So, short answer, it's just another way of providing some more detail and organization to your classes.

Robust Tran
Robust Tran
2,802 Points

Thanks for you quick response. As you explained I think that means whenever I execute "db.connect() and db.create_tables([Diary])" line, the peewee will check for the database (Sqlite) following by Diary.Meta to get the right one right?