Class & Object in Python

Python is a Programming Language which implements the concepts of object-oriented programming. Features like encapsulation, abstraction, inheritance, classes and objects increase the reusability of the codes and decrease the complexity.

The object-oriented technique allows you to create a pattern for a particular task and use it repeatedly in the program. This is something known as the concept of class and object.

What is Class?

A Class can be thought of as a layout or blueprint or structure or simply a pattern which is defined for the objects of that class. This pattern shows two things:

  • Properties/Attributes: Properties of a class, technically called attributes indicate the characteristics of the objects belonging to that class.
  • Behaviour/Methods: As the name says, behaviour shows the functions performed by the objects of the class. When a function belongs to a particular class, it is termed as a method of that class.

For example, Animal is a class with attributes as nature, lifespan and methods as eats. Every animal has nature, a fixed lifespan and eats something.

What is an Object?

An object is the instance of a class. The object, which belongs to a specific class, it exhibits all the attributes and methods of the class.

For example, Tiger is an object of the class Animal whose nature is wild, lifespan is 18 years and eats flesh. Thus all the animals are the objects of the class Animal.

How to create a class in Python?

Creating a class in python is similar to other languages. A keyword class is used which is followed by the class name. This statement ends with a colon.

Syntax:
class Class_name:
     body of class

How to create methods in a class in Python?

Methods are the functions which are created inside a class. They are defined with the keyword def.

In Python, a parameter self is implicitly(automatically) passed to the method. This parameter later acts as a reference to the objects of the class. Apart from self, other parameters can also be passed to the function.

Syntax:
class Class_name:
     def method_name(self):
          body of the method

How to create the attributes in a class in Python?

A method called __init__() is used to create the attributes of a class. This method also takes the argument self implicitly. There can be any number of attributes in a class.

Syntax:
class Class_name:
     def __init__(self):
           self.attribute1 = value
           self.attribute2 = value

The values to the attributes can also be passed as argument.

Syntax:
class Class_name:
     def __init__(self, a1, a2):
          self.attribute1 = a1
          self.attribute2 = a2

How to create object of a class in Python?

An object is created with the help of the assignment operator in Python.

Syntax:
object_name = class_name()

How to call a class method?

Methods are called with the help of the dot (.) operator.

Syntax:
object.method()
or
class_name.method(object)

Now, let us create a simple program with class and objects.

class Animal:
     def __init__(self, name, N, L): # creating attributes
          self.name = name
          self.nature = N
          self.lifespan = L
     def eats(self): #creating method
          if self.nature == 'wild':
               print(self.name, 'hunts other animals and eats their flesh.')
          elif self.nature == 'pet':
               print(self.name, 'eats grass, it is a herbivore.')

tiger = Animal('Tiger','wild',18) #creating first object
cow = Animal('Cow','pet',22) #creating second object

tiger.eats() #object tiger calls the class method hunts().
cow.eats() #object cow calls the class method hunts().

Output:

Tiger hunts other animals and eats their flesh.
Cow eats grass, it is a herbivore.

Explanation:

In this program, a class named Animal is created. Inside this class:

  1. Attributes are defined with the help of __init__() method. You can see that three arguments are passed to it other than self, including name, N, L.
    Inside the __init__() method, three attributes name, nature and lifespan are created which take the values of the three arguments name, N and L respectively.
  2. A method eats() is defined which takes only one parameter i.e self. This method uses elif statement and tests the value of attribute nature for 'wild' or 'pet'. Based on the evaluation, a message is printed in the output.

In the next step, two objects are created namely tiger and cow. Since the __init__() method is defined with 3 arguments, you need to pass the required actual parameters at the time object creation. Thus, for each object, the given values get stored in the attributes.

For example, in the case of the object tiger, the attribute nature holds the value wild, whereas, in the case of object cow, the attribute nature holds the value pet.

The concept of class and objects makes a program more efficient and compact. A class is 'create once use everywhere' type of entity which allows you to reuse the code.