OOP for beginners :-)

Hi all,
I’m new to OOP and I’m struggling with the concept.
Let’s say i would like to create a program that takes information from the user (contract details like Supplier, date, price etc…) and saves it into a DB (i haven’t decided which yet).
What will be the best to start building the Class?
I know how to do it with Functions but i know that if i want to step up my knowledge in programming, i have to start using OOP.

Now, i know that the class object will be let’s say “Contract”, but inside in the ‘init’ function… the parameters will be all the user’s input? can it receive a list-type parameter?
Any help will be appriciated.

Some indication if there’s a class hidden somewhere, might be looking at the parameters of the functions. If there’s couple functions using the same parameters - that might mean they are all related and could be put together as methods for a new class.

I wouldn’t think of a class in terms of user input. Functions may be the best way to solve some problems.

You might want to use classes if you are creating a type of thing that will have multiple instances. Maybe you create a Class called Truck and it has a name, wheels, cargo manifest, capacity, a driver, all trucks will have these things.

Now that it’s a class you can quickly instantiate 4 trucks with different driver names, some have 4 wheels some have 16 wheels, you can update the cargo of each separately in a uniform manner.

Optimus = Truck(Chris, 4, 16, carrots)
Tanner = Truck(Bob, 16, 52, apples)

Now I have 2 trucks with all the usual properties and methods a truck has. One has 16 carrots on it, a driver named Chris and 4 wheels. One has 52 apples.

This is how I would think about OOP, generally speaking. This is a concrete example, and I think it can get a lot more abstract, but it’s a good starting point.

In @sanity 's example, maybe you have a cargo function tracking different cargo of different trucks, and a route function tracking routes, you might combine those into a Truck class so they are all within a group.

You can learn more here: https://www.freecodecamp.org/learn/scientific-computing-with-python/python-for-everybody/objects-a-sample-class

A class tends to make the most sense in the case that you need abstractions for objects that have many analogous features, or you want to represent a bunch of independent objects that have some trait of being the same ‘type’ in the real world.

The main benefits of OOP often aren’t that apparent until you’ve written some more complicated projects and you start to realize how much easier they would be t understand if you had implemented certain design patterns. In your case, I wouldn’t necessarily just set the goal to implement a class if it’s not necessary. At the beginner level, it often makes more sense to write your code how it makes the most sense to you (in algorithms this is something akin to the ‘naive solution’) and then from there try and figure out what design decisions you made poorly and how you can fix them.

1 Like

Thanks, everyone.
So i guess, in my case, it’s better to create this program with functions.
Now, that i’m thinking of it… if i want the program to be able to work with multiple DB’s, maybe here OOP will come in handy. Isn’t?

Not necessarily, it’s hard to say with confidence that in an abstract situation x or y classes will be useful. To some extent class is convenience of having data and methods together, without the need to pass around data as arguments to each related function.

In context of python, there’s a great talk about classes, with
a bit funny title Stop Writing Classes https://www.youtube.com/watch?v=o9pEzgHorH0

Very interesting :slight_smile:
Well, thanks to everyone. You have helped to decide how to continue with my program.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.