Introduction to OOP

Object-Oriented Programming refers to creating classes and objects. In this article, we will learn about the basics of Object Oriented Programming in python and proceed to some advanced concepts in the next articles.

Till now, we have been learning functional programming in python. You must know some facts about functional programming because it will allow you to understand the importance and features of OOP better.

Facts about functional programming

1. Every new functionality requires a new function

2. Complex to identify the bugs

3. Difficult to manage and understand the code of complex programs

OOP resolves all these problems by providing a very beautiful structure of programming. So, there are two main entities with whom we work most of the time.

Classes

Class is a blueprint of an object. We can consider a car as a class and its details like color, model, and engine as its objects.

Objects

The object is an instance of a class. When a class is defined, the objects are only described in the memory and no memory allocation is performed. The real-world example can be the color, mode, and engine of a car as the objects of the car.

Creating Class in Python

Constructor function

Before creating a class in python, let’s understand the built-in function __int__(). All the classes have this function called __int__(). This function executes every time a class is initiated. We can call this function the constructor of the class. This constructor can be used to assign values to the object properties or some other necessary operations which you want to perform when the objects are being created.

Example

class Car:
  def __init__(self, color, model):
    self.color = color
    self.model = model

c1 = Car("Red", 2022)

print(c1.color)
print(p1.model)

In the above example:

We create a class Car and defined a constructor function.

The constructor function accepts three parameters self, color and model.

The constructor function assigns the color and model to the car object.

We create the car object and print it.

Note: The self keyword refers to the current instance of the class. So, to access the color and model properties of the class, we use the self keyword. It is not mandatory to call this variable self, you can give it any valid name, and must be the first parameter of the function.

The __str__() function in class

The __str__() function decides the returned value when the class object is represented as a string. If this function is not defined, the objects are returned as strings.

Example – without using __str__() function

class Car:
  def __init__(self, color, model):
    self.color = color
    self.model = model

c1 = Car("Red", 2022)

print(c1.color)
print(p1.model)

Example – with __str__() function

class Car:
  def __init__(self, color, model):
    self.color = color
    self.model = model

  def __str__(self):
    return f"{self.color}({self.model})"

c1 = Car("Red", 2022)

print(c1.color)
print(p1.model)

Creating methods in class

We have learned to create classes and objects. In this section, we will learn how to create methods in class and call these methods using the objects of the class.

class Car:
    
    # instance attributes
    def __init__(self, color, model):
        self.model = model
        self.color = color
    
    # instance method
    def accelerate(self, song):
        return "{} accelerate {}".format(self.name, song)

    def race(self):
        return "{} is now racing".format(self.name)

# instantiate the object
c = Car("Black", 2010)

# call our instance methods
print(c.accelerate("'100km/h'"))
print(c.race())

In the above example

We create a class Car and added two functions accelerate() and race()

Create the car object.

Call the accelerate() method using the object created.

Then call the race() method.

You must practice this example and check the output of the program.

There are some special attributes that perform specific functionalities. Let’s learn __doc__() attribute.

__doc__() attribute

Thus attribute returns the docstring of that class. A new class object is created as soon as we create a new class. This object is used to create new objects of that class as well as to access various properties/attributes of the class.

Example

class Car:
    "This is a car class"
    color = "Black"

    def maxSpeed(self):
        print("120km/h"')


# Output: 10
print(Car.maxSpeed)

# Output: <function Car.maxSpeed>
print(Car.maxSpeed)

# Output: "This is a car class"
print(Car.__doc__)

Categorized in: