codewithjohn.dev
Published on

Object Oriented Programming in Python

Table of Contents

Python is one of the most popular high-level programming languages and is commonly used for developing websites and software, task automation, data analysis, machine learning and data visualization. Almost everything in Python is an object In Order to understand python class and object lets first define both the meaning of Class and object.

object-oriented programming language is a type of programing paradigm that involves grouping of variables(properties) and methods(behaviors) in to a uint and we call that unit an object

In object oriented programming language class can be defined as a blueprint where objects can be created from and An object is simply a collection of variables and methods that act on those data. Ones we created the class we can create many objects from the class this objects are called Instances of a class and the process of creating the object is called object instantiation If you want to know to write Code that Scale and That is more readable you should be familiar with object oriented programing in python

For example: in real life, a Car is an object and the car has attributes, such as weight and color, and methods, such as drive and brake.

Defining a Class in Python

If You are already familiar with python we all know that to define a function in python we use the def keyword. class definitions begin with a class keyword. which is followed by the name of the class and a colon at the end. Any code that is indented below the class definition is considered part of the class’s body. So when we are creating a class we are creating new local namespace where all its attributes are defined. Attributes may be data or functions.

Here’s an example of a Car class:

class Car:
  pass

We are using the pass keyword because Empty code is not allowed in loops, function definitions, class definitions, or in if statements.The pass keyword is used as a placeholder for future code

Now let’s define a class with some variables and methods

class Car:
  color = "black"
  model = 2022
  def drive(self):
	  print("driving")

Creating an Object in Python

In The above example we have created a class called dog with variable color black and model of 2022 and with a method called diver so we can use this class To create many instances or objects.

As we mentioned above Creating a new object from a class is called instantiating an object. You can instantiate a new object by typing the name of the class, followed by opening and closing parentheses

let’s create an object of the above Car class

mycar = Car()

This Will create a new object instance named mycar. We can access the attribute of the object using the object name prefix followed by dot

mycar = Car()

print(mycar.model) # 👉️: 2022
print(mycar.color) # 👉️: black
print(mycar.drive()) # 👉️: driving

Constructors in python

Constructors are used to initialize the object’s state. Constructors executed at the time of Object creation. In python we can use the __init__() special method to create a constructor. Constructors are responsible for assigning values to the data members of a class when an object is created.

The __init__() method is called automatically every time the class is being used to create a new object.

Let’s update the Car class with the __init__() method that creates name and age attributes:

class Car:

  # init method or constructor
  def __init__(self, name, color, model):
      self.name = name
      self.color = color
      self.model = model


  def drive(self):
    print('driving')


mycar = Car('Tesla', 'black', 'Model S')
print(mycar.name) # 👉️: Tesla
print(mycar.color) # 👉️: black
print(mycar.model) # 👉️: Model S
print(mycar.drive()) # 👉️: driving

The self Parameter In Python

In The Above Example You may have noticed the self parameter in function definition inside the class but we called the method simply as car.drive() without any arguments. It still works. The self parameter is a reference to the current instance of the class. It is a way to access and manipulate the instance variables and methods of a class.

When you define and method in side Python a class the self parameter should be the parameter. By convection the name self is used but you can name it what ever you want