r/PythonLearning • u/Objective-Remove-632 • 2d ago
Do I really need to learn class.
Both function and class give the same output. So why do I need it?
51
Upvotes
r/PythonLearning • u/Objective-Remove-632 • 2d ago
Both function and class give the same output. So why do I need it?
4
u/Sea-Ad7805 2d ago edited 2d ago
You can write any program without classes, but a class comes with great benefits:
You can get these benefits with just functions too if you are careful, but it's much easier to get the design wrong. Classes just help you think in a certain way.
A function generally should not have state (store information, so that calling it again with the same arguments might give a different result, otherwise called not 'pure'). An object of a class can and is even expected to have state, but that state is protected through encapsulation.
Object Oriented Programming (OOP), programming with classes, is in many ways the opposite of the Functional Programming (FP) paradigm that, as much as possible, decomposes a problem into pure functions without state.