You want to learn Python OOP for a long time but you keep postponing? Or you are stuck in tutorial hell and do not code much from yourself?
No more follow along tutorial, now begin real coding! For the next 30 days I will post Python OOP exercises + solution.
Your challenge is to try to solve the exercise without looking at the solution. You can use the web to search for concept but please dont look at the solution before at least try to solve the exercise by yourself.
We will start very easy but don't be bored because exercises will quickly became more and more challenging....
You can also post your code in the comments and I will be please to review it and help you with any questions!
In OOP class is like the blueprint and object are the instance. For example a house plan would be the class but the houses that will be construct base on that plan will be the objects.
Your first challenge is to create a class name Product and create a instance of that class name product1
Since exercise #1 is pretty easy why not go for another one right now.
So we have a class named Product. A Product can have attributes like name, description, price, etc.
When the individual objects (product1, product2, etc.) are created, they inherit all the attributes and behaviours from the class, but each object will have different values for their attributes.
For the second exercise, add an object initialization method inside of the Product class.
With that init method get as parameter the name, description and price and assign them to instance attributes of the same name (hint: The current instance is reference by 'self' keyword)
After that create two object (product1 and product2). When you will initialize the object ex. product1 = Product('iPhone 12', 'This is a great iPhone', 799.0) you pass you three parameters (name, description and price)
Ready? Let's do it now!
Hint: If you are new to Python OOP. You can start by reading this: https://www.w3schools.com/python/python_classes.asp
STOP... Do the exercises first! It's the only way you will really learn.
If you have done your exercise here my solution. Noted most of the times, more than one solution will be possible. If you have something different that me, leave your solution in the comment to share and discuss.
Exercise 1 solution:
# Create a empty class # By convention, the class name is CamelCase class Product: # the pass statement allow the empty class with no error pass # Instantiate the class into a object # Note in Python there is no 'new' keyword product1 = Product()
Exercise 2 solution:
class Product: # class constructor # self keyword represent the object instance def __init__(self, name, description, price): # Set the object instance self.name self.name = name self.description = description self.price = price # Create new instance and passing properties with constructor product1 = Product('iPhone12', 'This is an phone', 599.0) product2 = Product('iPhone12 Pro', 'This is a PRO phone', 999.0) # Print in console print(product1.name) print(product2.name)
That's it for today. Tomorrow the journey continue, see you later!
If you want to be contribute you can re-tweet this post on tweeter