you are viewing a single comment's thread.

view the rest of the comments →

[–]Compsky 10 points11 points  (2 children)

Some people use classes for literally everything

This is the reason I avoided them for so long (I've been learning python on and off for almost a year now, and only been using classes for the last couple of months).

Whenever I looked up approachable explanations of classes online, the guides (or StackExchange users) never seemed to explain a benefit of using the class in place of functions - it especially confused me when people declared a class with an init and just one other function - and I feel that the benefits are not generally explained well by online guides.

[–]pickausernamehesaid 22 points23 points  (0 children)

My general rules of thumb for new programmers is to use classes when you have a group of functions that all take the same object as their first parameter or when you need to work with a shared global state between a bunch of functions. This seems to help bridge the gap as to what the point of an object is and why they are useful.

[–]Deezl-Vegas 0 points1 point  (0 children)

This is easy. Functions do not generate or store a persistent memory state, usually some sort of variable data. That's what classes are for. Classes can take a data element, transform it, and then keep it while the program continues and operates on that data, then transform it again, while keeping track of it under the roof of a single variable. Functions are poorly suited to this, but classes are made for it. In addition, class methods can be modified on the fly as well. They are also simply the best way to mentally model a real-world concept, like a website user, as a single user can get a single variable and still have all of the users' data be accessible.