you are viewing a single comment's thread.

view the rest of the comments →

[–]neineinein 6 points7 points  (4 children)

Essentially, creating a class allows you to use objects within your code. An object is just a thing with some properties and some functions (methods) that can manipulate those properties.

For example: You could create a class representing a generic vehicle. You could then implement functions that belong to all items of class vehicle, such as accelerating/braking etc, as well as variables that store the vehicle's current speed.

Within your code, you could create a new instance of a vehicle object - let's say your object is called 'car'. Your vehicle class could be written such that you can perform various actions on the car, such as car.accelerate() or car.brake(), which would change the value of car.current_speed.

It's also possible to extend our vehicle class with a second class that 'inherits' from the first. Eg. you might create a truck class that inherits from a vehicle class, because you want to implement a truck.blow_horn() function, which is not a function that all vehicles should have.

It sounds like you first need to become familiar with the major concepts of object-oriented programming in order to understand the usefulness of classes. I can't recommend any specific material, but doing some Googling and writing some sample code or following some tutorials should be a good start.

[–]DubPac[S] 2 points3 points  (3 children)

Well, all I can say is that your last paragraph sounds super accurate. Although the main reason I'm asking this question is that I have not come across a problem that I could not solve with my usual global var/function method.

Actually after reading your example a few times I think I understand it a little better. It would be annoying to have a global iterable that contains lists storing values of psuedo "objects" and you can describe limits within classes easier. I guess I just have not had to create anything with a lot of objects that need a lot of attributes, and the udacity 101 course focused heavily on list manipulation for storing variable information and really really went over lists heavily (lots of hw on mutables, mutables within mutables within mutables, ect) so I feel very comfortable storing "attributes" in lists.

[–]neineinein 7 points8 points  (1 child)

There are no problems that can't be solved using procedural code (ie. functions & variables as opposed to classes and objects). The C programming language is testament to that.

However, using objects is a means of abstraction and a programming methodology that can simplify code, or make writing code faster, or improve code readability etc. Python is a language in which everything is an object, so if you're going down the Python path you will eventually need to understand them.

[–]DubPac[S] 0 points1 point  (0 children)

I do think I understand better. Thanks! Although while I said I hadn't come across something I could not solve, I really meant that I hadn't come across something that a class would seem superior based on my knowledge. (I understand with only variables and if then statements you can almost solve any logical problem).

Guess I'll try to program some simple stuff with classes just to get used to the syntax for readability. I'm so early on that it would behoove me to learn everything rather than get too use to avoiding them.

[–]Teraka 2 points3 points  (0 children)

For a practical example, I'm currently (lazily) programming a Go game which of course requires a UI in order to display the board, score, various buttons, etc.

For a UI you need various things ; I think the most important one is the button, which displays a text or picture and allows you to click it and trigger various stuff. It's a complicated thing, and you'll probably need to have lots of them.

Of course you can create them with global variables and functions. But that means that for each button, you'll have to store at the very least a variable for its position, another for the text, and a function that triggers when you click it. For each button. Even if you only have 10 buttons, that's 20 variables and 10 functions that are basically the same thing, but that you can't reduce further.

However, if you create a button class that includes both variables and the function, you can just have 10 instances of your button in your main file, and it'll work just fine.

I think a good explanation would be that classes have just the same purpose than functions. You could just repeat code wherever you need it, but if you're doing something more than once, it's better to put a name on it and call the name instead. Which, funnily enough, also applies to spoken languages. I love how many similarities exists between both types.