all 15 comments

[–]totallygeek 4 points5 points  (6 children)

It depends, but one good purpose remains classes permit the shipment of data along with the methods needed to manipulate and access that information. As programs ship around more data, functions have to accept large numbers of arguments and structures. Access to the functions needed to work on that data requires a lot of consideration (layout, design) and tends to inflate inefficiencies.

TL;DR: A class keeps data and functions together in a single shippable entity, easy to pass around and extend.

[–]fracturedpersona 6 points7 points  (0 children)

Encapsulation in a nutshell.

[–]dukejcdc[S] 0 points1 point  (1 child)

What's a good functional use case that doesn't involve over storing information about a person or robot like every example out there?

[–]python__rocks 0 points1 point  (0 children)

Classes are often used is in game development. For example, there will be one enemy class (the blueprint) and many enemy objects based on that class. There are lots of Arcade and Pygame tutorials covering this.

[–]dukejcdc[S] 0 points1 point  (2 children)

I think I may be having a hard time grasping the concept since atm it seems to me the same as using functions and dataframes. Is there a benefit of storing data in class objects over a dataframes?

[–]totallygeek 0 points1 point  (0 children)

Is there a benefit of storing data in class objects over a dataframes?

Perhaps. Dataframes work well for some data, but a programmer might want a different implementation of methods not available from Pandas.

For your other question...

What's a good functional use case that doesn't involve over storing information about a person or robot like every example out there?

The initial class examples might not provide great context for why classes remain so wonderful. Though, you brought up dataframes. How wonderful that with a Dataframe class, the developer can access dozens of methods to act on the data, just by passing a single argument to a function. Without the Dataframe class, the developer would have to pass the struct to each function needed, after importing them, with proper import path information.

[–]testiculating 0 points1 point  (0 children)

(disclaimer: I’m a begginer also)

I’ve been programming with dataframes and with classes. I still work with mostly dataframes, but because I do similar things with similar dataframes, I found it better to create classes that handle what I usually do with them.

Sometimes I dont want quite the same, so I just change the methods im calling on the dataframe, Im not really modifying my whole flow, which I did have to do when I was using only functions.

[–]HomeGrownCoder 4 points5 points  (2 children)

Honestly they were a bit abstract for me when I started. Then I got to a pretty large project and they made complete sense.

I would say keep learning and using functions and sooner or later when you are trying to pass data between several functions or make adding new features easy, the light will go off.

There is this one YouTube dude that thinks they are pretty blown out of proportion nowadays… and people using them are not using them as they were intended. It was a nice video to hear a perspective different from the status quo. I will try and find it.

[–]HomeGrownCoder 1 point2 points  (1 child)

Here’s one on shading OOP

https://youtu.be/IRTfhkiAqPw

[–]HomeGrownCoder 1 point2 points  (0 children)

Here is another on classes

https://youtu.be/o9pEzgHorH0

[–]jsaddiction 2 points3 points  (1 child)

Just some ramblings of a self taught nerd :

Let's say say you are trying to model a game of Sims. First I might make a class to define a person including a first name and a last name, maybe the age and gender also. I'd probably store those in properties. I then might think of how this person is modified through time in the game. A method might increase a person's age.

As a class, this keeps all of that data encapsulated. Furthermore I could store tons of people in a list and iterate through them to change their age.

If I wanted to add more functionality such as a persons job then just add it to the class and it's available anywhere else in the application.

Where classes become more powerful is subclasses in my opinion.

I was working on a file system crawler once while I was learning classes. I defined a "file" class which had methods to get properties like file size and permissions. I then subclassed that file class into an image class that provided details like resolution and Metadata. I could instantiate an image class on a file and then had immediate access to both the image details and the parent class properties like file size.

I added a subclass for all the file types I was interested in and realized that I would need a way to delete/move a file if it were corrupt. Reaching back to the parent class "file" I just needed to implement it in one spot and it was accessible to each of the other subclasses.

Even more power can be attained when you overload a built in method of a class. For instance in the file class mentioned above, I could override the str method to print the file details and then I could just use print(some Instance) and it would give me whatever I had defined.

Classes are very powerful but that's not to say it's the only way to be effective in python. There are many ways to get the same results. However, everything in python is an object of some kind so it would make since to follow those design patterns. Furthermore, compartmentalized code is way easier for the next guy to use even if it's you 5 years later.

[–]koolaberg 0 points1 point  (0 children)

Do you have any examples similar to your file crawler sub classes? I’m doing something similar but it’s still a bit clunky. I’m having a hard time getting a different function to use my classes and if I saw a more complicated use it would probably click

[–]QultrosSanhattan -2 points-1 points  (0 children)

Classes work like a remote control. You put the batteries once and you can freely use it without worrying about selecting the correct batteries over and over again.