Python Object Oriented Programming in Nutshell
This paradigm seems kind of hard for beginner and mid-level programmers. It is a relief rather than a burden for us. Believe it or not, developing a product without OOP is similar to building an apartment with only one builder. In IT, there is a term called Procedural programming and it uses a linear top-down process. Imagine you start writing from line 1 to the last line that you stop writing. However, in this way when you are working on long and complex projects, the readability and efficiency of the program decrease.Our code becomes very like spaghetti.This method belongs to old programming languages like Fortran.While you try to connect all the items, it becomes like Kardashian’s dating history 😄
For this not to happen we need to divide our program into modules and classes. Here comes Object-Oriented Programming, which is very versatile in contrast to procedural programming. Let’s say you started in Starbucks as a software developer and your task is to create an algorithm for a coffee machine. Take your time and think about this for a couple of minutes!
We need a couple of functions to make it work.
- to check if the water and milk is enough
- to count the coins inserted
- to give the changes back
- make the coffee
- deduct the resources used
You can write these functions from top to bottom. Somehow you can connect them but as I said it would be messy.Let me show you an example of this👇 This simple coffee machine project was developed with procedural programming.
Get BAVI-BOOP’s stories in your inbox
Join Medium for free to get updates from this writer.
With OOP you can create a class for each of them in different modules. Instead of one player, we prefer teamwork with many players. If we depend on one module then we limit ourselves and come across problems. When all the modules finish, they all work in the main module. The main module serves as a gathering point for all functions.
What is more important is you can use these modules again and again in different programs. Each class serves a function and is reusable. For instance, we can use the money counting module in the supermarket algorithm or we can use make the coffee module in a bar program. It increases the productivity and readability of our program.
Coffee machine written with OOP👇
https://github.com/BAVI-BOOP/100-days-of-python/tree/main/day-16
Probably you are wondering how you can write these modules and classes. First, you need to understand the mentality behind it. I already tried to explain to you but in case you didn’t understand please check other resources. Secondly, you need to create another python file beside your main.py. Then you have got to define the class with this syntax. Class naming should be PascalCase.
What is a class? It is like a blueprint for you. You can use the class and create infinite instances. These instances are objects. Every object has a unique place in the computer’s memory such as <__main__.Test object at 0x2aba1c0cf890>. Using objects with coffee machine class you can create coffee machines with different prices different colors different coffee types with different resources.
Functions inside classes are called methods and variables inside classes are called attributes.
I stop here because I don’t want to dig deep and I expect you the understand the fundamentals. You can learn all the rules and syntax by googling. However, I get so many questions about the keyword self and __init__ keywords. For the last topic, I’ll try to mention it.
__init__ is one of the reserved methods in Python. In object-oriented programming, it is known as a constructor. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. When you create an object from that class, it initializes itself and automatically assigns the instance attributes. Instance attributes are defined inside a constructor using the self parameter.
The self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. You can assign different attributes to every object you are creating. Arguments and parameters in functions help us to achieve it.
For a better understanding, study a bit more on this topic and read this article one more time. You will see the difference. Thank you so much for reading until the end of the article. Let me know you liked it or not 👇





