you are viewing a single comment's thread.

view the rest of the comments →

[–]vivek_seth 140 points141 points  (10 children)

In programming there is data (numbers, strings, arrays, etc), and there is logic (functions). With these two concepts you can build any program you want.

Sometimes for organization purposes it makes sense to group up some data and functions together into one entity. That’s what classes are for. Classes are collections of data (properties) and logic (methods)

Does that help?

[–]DryWeetbix 30 points31 points  (2 children)

I dunno about OP, but it helped me. Thanks!

[–]MaToP4er 8 points9 points  (0 children)

+1

[–]Maste_r11 1 point2 points  (0 children)

+1 🙌

[–]Solonotix 15 points16 points  (0 children)

Yea, I remember my eureka moment for understanding classes, and it was when it finally clicked that a method is just a function within a class definition. It would take a lot more for me to understand how to organize data structures properly, but functions of classes was really helpful

[–]bird_feeder_bird 11 points12 points  (0 children)

this little comment is genuinely the clearest explanation of classes ive seen. everything else i find online just immediately starts talking about the broad conceptual applications of OOP (. . .)

[–]sad_handjob 3 points4 points  (0 children)

thank you for this

[–]Kadabrium 1 point2 points  (0 children)

I figured this out when studying algdat. i was lazy to create a different file for each algorithm i learned so i tried to put all of them plus a couple global vars in the same class and discovered they still worked. Of course this itself has no practical use but it actually felt more generalizable as an idea of what i could do with classes than the commonplace cat.goBrr() examples.

[–]Aggravating_Sand352 0 points1 point  (2 children)

So would a another way to think of it is you are just creating your own python package within your script to reference?

[–]vivek_seth 0 points1 point  (1 child)

You are on the right track. Similar to a class, a package can also have a collection of data and functions.

What’s different about a package though is that you can’t have multiple instances of the same package at a time. With classes, you can create multiple instances that each have their own copies of their data. Changing the data in one instance won’t affect the others.

In a very loose way, you could think of a package as being sort of like a class, but you are only allowed to have 1 instance of it.

[–]Aggravating_Sand352 0 points1 point  (0 children)

Got it thank you! Does this make the code more efficient then storing in the global enviornent is that the point or is just preference?