DEV Community

Cover image for Notes to get into OOP in Python
Ayaan Panda
Ayaan Panda

Posted on

Notes to get into OOP in Python

Object Oriented Programming(OOP) is really powerful and really good to use, especially on big projects, here are some notes on OOP in Python. Remember, you should know Python Basics, if you don't, check out this other post, By the way, I have a video series on OOP in Python, here's the first video if you wanna check that out Anyways, here are the notes!!

OOP - OOP means Object Oriented Programming. Object Oriented Programming is when you do programming with Classes, Objects, and Methods

Class - You can think of a class as like a Data Type. All data types like strings, floats, or lists, are all classes. When you create a new class in your program, you're creating a new data type.

Methods - A method is a function that is part of a class. For example, strings have a method you can use on them called replace. Well replace is a method of the class string. Similarly, you can add methods to your own classes.

Object - An Object is an instantiation of a class, so if you had x = "Something", x would be the object.

Self Keyword - All methods need to have one parameter, and that is the self parameter. Self is a keyword in python that refers to the Object that is created from the class. For example, lets say you had x = "A String". Well, in the string class, self would refer to that x object. With the self keyword, you can attach attributes to that object, so for example, lets say you did self.likesCoding = True, and you made an instance of that class called x. Now, if you do x.likesCoding, it should give True.

init - All classes need to have the init method. Whatever you put in the init method will happen whenever an object of that class is instantiated. With the init method, you can actually make it so that whenever you instantiate the object, it will actually take in some arguments. For that, you just pass in those parameters in the init method.

Class Syntax:

class Dog:
    def __init__(self, name, age, breed): = name
        self.age = age
        self.breed = "breed"
    def bark(self):

x = Dog("Jeff", 5, "Havanese")
print( # This will print Jeff
print(x.age) # This will print 5
print(x.breed) # This will print Havanese
x.bark() # This will print Bark
Enter fullscreen mode Exit fullscreen mode

Inheritance - Lets say you had to classes, Dog and Cat. The only difference between these 2 classes is that Dog has a bark method, and Cat has a meow method. It would be very repetitive. For this situation, you can use Inheritance. Inheritance is a way so that you can have child classes inheriting from a parent class. So the child classes would have all the same things as the parent class, plus it's own things.

Inheritance Syntax:

class Pet:
    def __init__(self, name, age, breed): = name
        self.age = age

class Dog(Pet):
    def bark(self):

class Cat(Pet):
    def meow(self):
Enter fullscreen mode Exit fullscreen mode

So that is Object Oriented Programming in Python. You're most of the time going to use OOP in big and production level code. OOP is a must-know in programming. I hope these notes helped!

Top comments (0)