DEV Community

Cover image for OOP Basics in Python.
devcat
devcat

Posted on

OOP Basics in Python.

Object-oriented programming (OOP) is a programming paradigm that is centered around objects, which are instances of classes that encapsulate data and behavior. Python is an object-oriented programming language that fully supports OOP concepts such as inheritance, encapsulation, and polymorphism.

In this blog post, we will cover the basics of OOP in Python, including defining classes, creating objects, and using inheritance to create subclasses.

Defining Classes

In Python, a class is defined using the keyword "class" followed by the name of the class, and a colon. Here is an example of a simple class definition:

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

    def bark(self):
        print("Woof!")

Enter fullscreen mode Exit fullscreen mode

In this example, we have defined a class called "Dog". The class has two attributes, "name" and "age", which are set in the constructor method, "init". The class also has a method called "bark", which simply prints out the string "Woof!".

Creating Objects

Once a class has been defined, we can create objects, or instances, of that class. Here is an example of creating an object of the Dog class:

my_dog = Dog("Fido", 3)

Enter fullscreen mode Exit fullscreen mode

In this example, we create an object of the Dog class called "my_dog". We pass in two arguments to the constructor method, "Fido" and 3, which will set the "name" and "age" attributes of the object.

Using Methods

Once an object has been created, we can use its methods. Here is an example of using the "bark" method of the "my_dog" object:

my_dog.bark()

Enter fullscreen mode Exit fullscreen mode

When we call the "bark" method on the "my_dog" object, it will print out the string "Woof!".

Inheritance

One of the key features of OOP is inheritance, which allows us to create subclasses that inherit properties and behavior from their parent class. Here is an example of creating a subclass of the Dog class:

class Labrador(Dog):
    def __init__(self, name, age, color):
        super().__init__(name, age)
        self.color = color

    def swim(self):
        print("I'm swimming!")
Enter fullscreen mode Exit fullscreen mode

In this example, we have defined a subclass of the Dog class called "Labrador". The class inherits from the Dog class using the syntax "class Labrador(Dog):". The class has a new attribute called "color", which is set in the constructor method. The class also has a new method called "swim", which prints out the string "I'm swimming!".

When we create an object of the Labrador class, it will have access to the methods and attributes of both the Dog class and the Labrador class. Here is an example of creating an object of the Labrador class:

my_lab = Labrador("Buddy", 2, "black")

Enter fullscreen mode Exit fullscreen mode

In this example, we create an object of the Labrador class called "my_lab". We pass in three arguments to the constructor method, "Buddy", 2, and "black", which will set the "name", "age", and "color" attributes of the object.

We can use the methods of both the Dog class and the Labrador class on the "my_lab" object. Here is an example of calling the "bark" method and the "swim" method on the "my_lab" object:

my_lab.bark()
my_lab.swim()

Enter fullscreen mode Exit fullscreen mode

Remember that OOP is just one of many programming paradigms, and it may not be the best approach for every situation. However, it is a useful tool to have in your programming toolkit, and can help you create more maintainable and scalable code.

I hope this blog post has been helpful in introducing you to OOP in Python. Keep practicing, experimenting, and exploring, and you'll soon become an expert in Python's OOP features.

Happy Coding!

Top comments (0)