Getting stuck with Object Oriented Programming (OOP) in Python ?

Photo by RetroSupply on Unsplash

Getting stuck with Object Oriented Programming (OOP) in Python ?

·

3 min read

You might have come across object-oriented programming when using Python. Object Oriented Programming is a method used to structure computer programs by grouping related properties and behaviors into objects. Let's explore objects and classes in Python.

Classes in Python

Object Oriented Programming in Python involves grouping programs in such a way that properties and behaviors are bundled into individual objects. We can use the concept of classes to represent real-life objects. A class in Python is a blueprint for creating objects. It defines the data and behavior that all objects of that class will have.

For instance, an object could represent a person with certain properties and behaviors. Properties like name, age, and address can be used to describe an object in Python. Check out the example of a class and an object in Python3.

Class Dog:
      pass

dog = Dog()

In the example above, there is a class classed Dog and we create an instance of the class. The instance of the class Dog, is called an object. Let's talk about constructors in Python.

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

We have created a method in the class called Dog. The __init__ method defined is called a constructor in Python. This has two properties called name and age. In the body of .__init__(), there are two statements using the self variable:

  1. self.name = name creates an attribute called name and assigns to it the value of the name parameter.

  2. self.age = age creates an attribute called age and assigns to it the value of the age parameter.

Attributes created in __init__ are called instance attributes. An instance attribute’s value is specific to a particular instance of the class. All Dog objects have a name and an age, but the values of the name and age attributes will vary depending on the Dog instance.

Instantiate an object in Python

To instantiate an object in Python, you need to create a variable and set it equal to the class name with parenthesis.

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

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


dog = Dog("Spot", "Labrador Retriever")

In this example, we create a class called Dog. The __init__() method is the constructor method, and it is called automatically when an object of the class is created. The bark() method is a regular method that can be called on an object of the class. Let's look at another example below:

print(dog.name)

We can now access the attributes of the dog object using the dot operator. For example, the code above prints the name of the dog.

I hope you found this blog post useful. In my next blog post, we will discuss classes and objects in more detail. :)

Did you find this article valuable?

Support Iqra by becoming a sponsor. Any amount is appreciated!