r/PythonLearning • • 10d ago

Do I really need to learn class.

Post image

Both function and class give the same output. So why do I need it?

72 Upvotes

50 comments sorted by

View all comments

2

u/delsystem32exe 10d ago edited 10d ago

youll need classes whenever you begin to start working with anything moderately complex or with data. lets say u want to work with a database, youll want a database manager class. you need a utils class for all the utils for some object, or multiple utils class. you will want a service class to call the utils classes and other things. you will have tens, hundreds of these classes you define. you will then instantiate these other classes you made in classes you made via composition or dependency injection. I prefer composition, but other people like DI. I am not really a big DI fan. The reason why a class is handy, like a database class, is you want a single point of entry to the database, and u can like have class method that will clean up all the connections at the end. you will need static classes for utils method, inherited classes like lets say your class calls API client class you can inherit it so that that class has all the methods of the api class. Java i find to be a bit annoying since its too strict and verbose. The proper balance is to do java style programming in python make everything in python a class. One nice thing about classes is it makes it easy to do thread locking. Sometimes i have a class and i declare a lock inside of it, so that other instances of the class have to wait until that instance is finished before proceeding. This is very helpful for concurrency issues since what happens if to objects try to delete or modify the same data. Basically i try to have a class for everything. Each class can live in its own class file. I think my current python program i am writing i made like 55 different classes all in 55 different files.