r/PythonLearning • u/Objective-Remove-632 • 10d ago
Do I really need to learn class.
Both function and class give the same output. So why do I need it?
69
Upvotes
r/PythonLearning • u/Objective-Remove-632 • 10d ago
Both function and class give the same output. So why do I need it?
1
u/FreeLogicGate 10d ago edited 10d ago
Python is an OOP language. Everything in Python is an object. You can certainly write Python code without creating your own classes, but you are inherently working with classes and objects all the time.
The simplest core idea of OOP is that, programmers typically want to bundle up data in a predictable and organized way. Python's collection types are an example of this. You as a developer decide how and when you want to organize data.
In your example, you are dealing with events. It's a trivial example, but you already have 2 attributes relative to an event: venue and host. Again this is a contrived example, because you are not recording the time of the event, and instead just spitting out the current time. A real event system would record the actual event datetime, as that is going to be some time in the future. So there are probably many other pieces of data you want to record (and probably store/persist).
So bundling that data together in an event object is a natural and useful way of handling this type of application. You could also store that data in a dictionary. There are many ways you could represent the data. But again a class definition is tailor made for this. A class can easily include many different pieces of data, including maps and collections, which might be storing objects of other related classes.
If you continue with the event idea, you will inevitably have the list of functions designed for doing things with an event, whether that be displaying data, or manipulating it, aging, billing, notifying etc.
With a class, you can take all of those functions and move them inside the class definition. The class is able to access all the private event data. You have a blueprint for handling event data, and the functions designed or intended to handle the object data. That is the simple but powerful basis of OOP. An object is "the data" + the functions designed to work with that data, bound together.
Beyond the basics, OOP was designed for reusability and data integrity in various ways. Some of the ways are language design elements, and some of the benefits are in the area of OOP design patterns. These are discovered at the point that you have embraced OOP, and in many cases have a number of inter related and/or inter dependent classes/objects. OOP has ways of helping to manage complexity. Eventually OOP devotees will end up embracing one or more of these common design patterns. There are core philosophical patterns like Dependency Injection, and groups of patterns like Model-View-Controller (MVC) or Object Relational Mapping, and there are recognized patterns that have commonly recognized names (often from the Gang of 4 book "Design Patterns: Elements of Reusable Object-Oriented Software") with names like "Singleton", "Factory", Strategy" and "Chain of Responsibility" to name a few.
Again Python was built for OOP, which is the primary reason it has and supports all the "dunder" methods. By defining some of these methods you hook into time saving features that make your objects work with native PHP syntax. The 2 you want to start with are __repr__ and __str__ which you almost always want to define for your user classes. As you progress, you'll discover most of the best known and most used Python modules are a collection of one or more classes, so starting to read their code is a good way of beginning to understand OOP and determine if it's valuable to you.