Understanding Class and Object with an Example in Python

In this class, we had an Understanding Class and Object with an Example in Python.

For Complete YouTube Video: Click Here

Class and Object Example

The reader should have prior knowledge of functions. Click here.

Before going to the concept of class and object, we take an analogy and understand the concept of class and object.

The analogy we consider here. Construction of a building or house.

To construct a building or house. First, we design a blueprint. And then we go for construction.

Understanding Class and Object with an Example in Python1
Blueprint

A sample blueprint for a house is shown in the diagram given above.

The blueprint shows the area of the dining room, size of steps, etc.

By looking at the blueprint, we start constructing our house.

During the construction, we physically assign space to the house. Then, in that assigned space, we start our construction.

For example, the house is constructed in India with sand and cement.

The above-constructed house is existence in the real world. I.e. the home is assigned space in this world.

With the exact blueprint, we constructed one more house in the USA using wood.

In the same way, we constructed one more house in the UK using mud and bricks.

From the above analogy, we understand the exact blueprint. We constructed three different houses in different places with different inputs.

Using the above analogy, we understand class and object.

Class:

Class is a user-defined blueprint or prototype of an object.

Take an example and understand how to define a class.

class rectangle:
     def __init__(l,b):
           length=length
           breadth=breadth
     def Area():
           A=length*breadth
           print(A)
     def Perimeter():
           P=2*(length+breadth)
           Print(P)
     def Diagonal():
           D=sqrt(length**2 + breadth**2)
           print(D)

In the above program, a class is defined. The class name is given a rectangle.

The class described above consists of four functions.

F1) __init__ this function is defining two variables length and breadth.

F2) area function to calculate the rectangle area.

F3) perimeter function to calculate the perimeter of a rectangle.

F4) diagonal function to calculate the diagonal of a rectangle.

The functions are written in the above class. We call them methods.

Class, we call it to the blueprint. Why?

We understand when we discuss object.

Object:

An object is an entity in the real world.

The object is physically existing in the real world. Therefore, we assign space to the object.

This object is similar to the house from our analogy.

Defining object:

M=Rectangle(1,2) here one is given to length, and two is given to breadth.

By defining the object, physical space is assigned in the memory and shown in the below memory diagram.

Understanding Class and Object with an Example in Python2
Objects in Memory

Assume object M is given memory location 50. Now M is referencing location 50.

We define one more object, K= Rectangle(5,7).

Another space is assigned to object k. For example, assume at memory location 80.

Object k is referencing memory location 80.

We can define any number of objects we required.

The point to understand. Each object has its own inputs.

When we call the methods present in the class, M.Area () will calculate the area for the inputs of 1 and 2.

K.Area() will calculate the area for inputs 5 and 7.

Class is a blueprint. And the object is an entity in the real world that applies class methods to the inputs provided to the object.

Real-World Example

Class google:
      def __init__(input):
           input=string
      def search():
           search websites related to input string

In the above program, input to the object is a string.

The search method written in the class will take the input string and search the result for the input and display the output.

When one opens the google search, the nearest server will create an object for the user.

The user search keywords are taken as input to the object. And search method is called.

For each user, a separate object is defined. So each user will have his own input space.