Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about classes
#1
Hi,

I am learning Python as my first programming language and I love it - I follow a course on Coursera and two others on Edx, plus tons of YT channels and such. Right now I am learning about classes and although I understand their functionality and how they work, I have some trouble understanding the necessity or the benefits of using them. Let me explain:
Basically, as far as I understand, I can do everything without using classes- and please correct me if I'm wrong. They seem to me just like over-complicated functions, or a kind of a function on top of some other functions- please correct me if I'm wrong. In one of the videos I saw, the instructor was saying that classes are meant to organize functions based on their common functionality, like having a class called "math ops" for instance where one can define all the math ops one desires . Which I get, but why is that different from using a simple .py module with all the various functions and import and reuse or extend them from there? What is best practice? When does one use classes vs. functions? If classes are "better"- which already sounds a bit dumb if I put it like this- or offer a better functionality, why don't we use them all the time? I did a huge amount of exercises, and there just a few of them that involve classes. Usually the solutions I see just use plain functions instead...I find it very confusing.
I'm sorry if this seems naive, I would greatly appreciate your advice on this!
Reply
#2
My two cents on this as a relative noob myself... Classes and instances/objects are the backbone of object-oriented programming, and there is a reason OOP has been the dominant approach to high-level programming for the last 40-50 years. For really simple programs, like the kind you might encounter in a course that are meant to illustrate the basics of using classes, the value of using classes versus separate functions and variables may not be readily apparent. When you have programs that go beyond a few hundred lines of code and get more and more complex, it becomes much more obvious why classes are needed.

To me, the easiest illustration of this is a video game. Say you are coding a video game where you want to spawn 50 enemy NPCs for the player to do battle with. Each NPC will be unique and will need to be uniquely referenced by your code, but they have a ton of traits in common. Each of them will require variables to store their name, health, position on the screen, weapon type, damage output, etc., and many of those variables need to be updated constantly. Additionally, each NPC will need to call the same functions on a regular basis (attack(), move(), and so forth).

Without using classes, you would need to manually code 50 sets of variables, one set per NPC. Each time you called a function for a given NPC, you would need to manually pass in all of the variables required by the function. This would be extremely time-consuming and inefficient, and nearly impossible to scale. What if you decide instead of 50 NPCs you need to have 500? The amount of time needed to do all the unique coding would be overwhelming.

With classes, you can create a class for your NPCs that contains all of the variables associated with each instance, and also contains all of the methods (class-based functions) you'll need. This way, you can spawn 500 NPCs just as easily as you can 50. The variables that all of the NPCs have in common are defined in the class. As you spawn NPCs, they will pick up default values for those variables from the class definition, so you only need to pass in variables you choose to pass uniquely for each instance. (Note that "spawning NPCs" in this case would equate to "creating instances of the NPC class".) Since your methods (functions) are built into the class as well, the methods can reference instance-specific variables without needing to manually pass in a ton of information each time. So when you call move(), you don't have to pass in any of the location variables that method needs to manipulate, because all of that information is already stored in an instance-specific variable that the method can access.

As to when to use classes and how to use them efficiently, that is something I still struggle with myself. Like with everything else, it gets easier as you practice. If you are interested, there is a really great set of video lessons by Corey Schafer on OOP in Python that starts with this video.
Reply
#3
Hi Goto10,

thank you a whole lot! That makes perfect sense now, very well explained! Not only that, but your analogy also helped me better understand decorators! Great!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020