When do we really need to use Classes in Python?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

When do we really need to use Classes in Python??

4th Mar 2019, 3:14 AM
Sunday Afolabi
Sunday Afolabi - avatar
4 Answers
+ 3
Classes are useful for structured programming & organization. Also, in order to obtain the benefits of Object Oriented Programming like encapsulation, data hiding, polymorphism, etc. Classes are required. They help reduce redundancy and increase the chance of reusability. With a scripting language like Python trying to define "when" is difficult especially if your program performs the task you intend. As your code grows and becomes more complex it is advised to optimize performance & perform code/peer review. However, if you are writing one off scripts, classes are not needed.
4th Mar 2019, 3:46 AM
Steven M
Steven M - avatar
+ 2
GUI libraries like tkinter and kivy are also written in OOP style (by classes). It may be beneficial to use classes: - when you deal with complex data structures that are difficult to represent with the builtin types like list and dictionary, even when nesting them - when the important aspect of your program is tracking the state of your objects and it would be cumbersome with pure variables - when you have a large codebase and it makes sense to structure and split it into meaningful, self-contained chunks Some languages like Java force everything into the OOP paradigm even when it doesn't make sense. In Python the choice is yours.
4th Mar 2019, 5:38 AM
Tibor Santa
Tibor Santa - avatar
+ 1
It's useful when you want to write the code once and use it multiple times with other sets of data and may be in some other codes.
4th Mar 2019, 3:30 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 1
Thanks, i just find it a bit hard to understand it
4th Mar 2019, 6:55 AM
Sunday Afolabi
Sunday Afolabi - avatar