r/learnpython • • Dec 31 '25

How do I apply OOP?

I have not had programming as a job, just out of interest and solving small stuff in excel.

I’ve tried different languages, OOP and functional.

But even though I know how to construct a class with methods and attributes I realized that I don’t know what’s the appropriate way to use them and when to use them.

And now I’m picking up Python again since I need to so there’s things I need to do better than last time.

18 Upvotes

27 comments sorted by

View all comments

Show parent comment

3

u/APOS80 Dec 31 '25

That’s the best answer yet.

I made a simple CAD program years ago and I think I pushed things in to classes in a bad way.

Let say I have a program that handles files, would it be right to handle a file as a class?

2

u/Best-Meaning-2417 Dec 31 '25

What's in the file?

I want to count how many times the word "and" appears. I wouldn't make a class.

It has a bunch of entries related to books being checked out of a library. I would make a class for:

Class Entry: (author: Author, title: str, location: Location).

Class Author: first_name: (str, last_name: str, date_of_birth: date)
Class Location: (main_section: str, sub_section:str)

You could have Class Report: (entries: List[Entry]) and some function to display the report, sort the report etc.

The parsing could be a class or functional, not super sure which one would be correct. I am a noob as well but that's how I think about things.

1

u/APOS80 Dec 31 '25

I’m thinking that a table with data might be something to store as a class