Basic OOP in Python 🐍📝
Published: 2023-05-19 09:56:35

Object-Oriented Programming (OOP) is oriented around objects that typically represent real world concepts. Data is being prioritized rather than functionality.
OOP provides a better way to structure and organize code, making it easier to read and maintain. You're essentially dividing up your program into small parts. 

What helped me solidify this concept is thinking in the literal sense. Since OOP is based on the real world, nothing is more real than you! (you are the realest) You are a person that has certain attributes that make you unique and behaviors. So let's start with that!


## person.py
![person.py](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4z76jgrvq59pmfga83u.png)

First I started real easy by just naming the class. My first method is to initialize this new object with its attributes. The first parameter will always be `self`. Self is a parameter that represents an instance of the class. It makes it so that we can access the attributes following up. Name and age are going to be our attributes for our person. 
In the body of our `__init__()` method are 2 statements. 
`self.name = name
self.age = age`
This is what is creating our attributes and assigns them with the value that is in the parameters. 
Next is a `greeting()` method. It will just print out a greeting. Using some string interpolation it will also print out `self.name` in the greeting. 

For the last 2 pieces of code, notice how I am now outside of my class. I'm creating an instance of the Person class. I just add in my name and age. Then I call a method from the Person class. 

So when I run my program I get back this in the terminal:

```
Hello! My name is Melissa
```

Alright sweet, now we can think about all the different things we can do with this person. This person has limitless potential. But for now, let's just create a Student class for it to inherit Person from. 
Let's think about what attributes a student would need. A name, age, and major. 

## student.py 
![student.py](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bapqdm6i42k81h5e106e.png)

The setup looks pretty similar to our Person. One major difference is the usage  of the `super()` method. On the top, I imported our Person class since they're 2 different .py files. Person already has the attributes of name and age. So to avoid typing all of that code again, I can just inherit those attributes using the `super()` method. 

This time I added 2 student instances. And when I run `python3 student.py` I get this in my terminal. 

```
Hello! My name is Melissa, I am 19, and I'm majoring in Software Engineering
Hello! My name is Kiana, I am 18, and I'm majoring in Law
```

##Conclusion 
This very basic idea can be further expanded upon. By thinking in objects, you can create classes that serve as blueprints. Our blueprint is the Person class. And then create other objects with similar attributes while still keeping the code DRY. Learning to incorporate OOP will make your coding more structured and easier to come back to. 


Great resources! :
https://www.geeksforgeeks.org/self-in-python-class/
https://realpython.com/python3-object-oriented-programming/
https://www.programiz.com/python-programming/methods/built-in/super