+ 1
When do we really need to use Classes in Python??
4 Respuestas
+ 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.
+ 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.
+ 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.
+ 1
Thanks, i just find it a bit hard to understand it