Hi there, I’m having a learning block from transitioning from functional programming (most of everything I’ve learned so far) to using classes. I have completed a couple of the classes and objects lessons in the “Scientific computing with Python certification” course, however, I’m still not “getting it” like I feel like I should be…
So, my question is: Does anybody know of any good online reading/resources that can help with the transition for me to start writing my code in object oriented ways using classes that uses functional examples as well as their class (OOP) equivalents?
So far all of the articles I’m reading have been using real world objects to show examples of OOP concepts and its not sinking in how to adapt a bicycle that has 2 wheels to a build a budget tracking app! As useful as the material may be, it’s not closing the gap I need to be able to be practical.
As always any help is much appreciated and thanks to fCC for everything you are doing!
The last thing I’d say about classes is that they make the most sense when you really NEED a class, not necessarily if you’re just practising them.
They make the most sense to me when you’re making a template and you will instantiate multiple instances of that you will keep track of separately.
For example you write a car class, with a change tires function. Then you instantiate 3 new cars, they each keep track of tire wear for themselves, internally. When one of them wears down car2.tirewear == 0.1 you run car2.changetire(4) to change all 4 tires on that car.
Generally goal of the class is to put together specific data with functions which can change that data. That can sound a bit abstract, because it is. Good indication when something might be needing a class is when few functions are having the same, or similar arguments.
@pkdvalis Thanks for your reply, you are always helpful!
That’s the thing, I don’t know what I don’t know
As stated I’m not sure how to bridge the gap from trying to do things in a linear way to doing the same thing with classes.
As far as I understand, a class is a way of grouping similar functions and variables that manipulate and/or organize data in a modular way (which is basically a module to me??)
I appreciate your patience and honestly I know it’s hard to answer a question when the person asking it isn’t sure what they are asking!
This is a big part of why it would not make sense. Often you don’t need a class, so it’s a bunch of extra complexity for no reason. Also has nothing to do with linearity, it’s just a way of grouping data and functions together and re-using them.
In a budget app:
An expense could be a class. So you are making an object that has internal variables like: cost, tax, category, date and internal methods like displaySummary or update.
When the business incurs a new expense you create a new object:
printer = new Expense(250, 30, “office”, todays_date)
Although, maybe it makes more sense to just store all of that in a big table or database that you query, so maybe you don’t need to use classes at all. So implementing classes here would be confusing since it isn’t necessary and it will make it hard to understand.
For me, the cars analogy works well bc it’s not so abstract. You have 50 cars that are all implementations of a class “car” so it’s easy to see how they have similar properties and functions, but they operate independently.
In a game:
Different bad guys in a game all have attack methods and hit points, so you would create a “bad guys” class. Each bad guy would use that class as a template and you fill in the different values for each new bad guy character.
The only reason I used that example is because of one of the certification projects: Budget App
I think I get what you are saying. Also, I have been trying to soak up a bunch of knowledge on the subject. I feel like I should be able to get a good start on the project and “fake it til I make it” until I research some more and hopefully be able to complete the project with a better understanding of the concepts of classes and objects.
I really appreciate all of the replies. I know that I was confused about what I was asking and that didn’t stop you fellow campers from trying to help me anyways. Thanks alot for everyone’s time!