In-depth Understanding of Classes and Object in OOPs with Python

Basic concepts for modular programming

Amit Chauhan
6 min readJul 16

--

Photo by ThisisEngineering RAEng on Unsplash

In this article, we will discuss object-oriented programming concepts. When we are working on a massive project it is important to make a code more sustainable and efficient. By making small modules of code, the programming becomes more readable and easy to interpret.

Topics to be covered

  1. Objects
  2. Class
  3. Encapsulation

Objects

What is an object?

  • An object always refers to the class. In python almost everything is an object, whenever we define a variable in actual we are creating an object that is related to some class in python.
  • The data type is a class and when we define a variable it becomes the object of that class.

Example:

Suppose creating a variable with different data types.

# variable types (these are normally object literal by built-in classes 
# available in python)

int_var = 5
str_var = “six”
list_var = [1,2,3,4,5]

# Let's see the type of these variables

print("Type of integer variable: ", type(int_var))
print("Type of string variable: ", type(str_var))
print("Type of list variable: ", type(list_var))

#output:
Type of integer variable: <class 'int'>
Type of string variable: <class 'str'>
Type of list variable: <class 'list'>

We can see in the above example that each variable of different data types belongs to some class. The time taken to create the class is more as compared to the object creation. The class is like a blueprint for how the object
will behave accordingly.

Class

It mainly consists of two components.

  1. Data or property or variable or fields or members or attributes*.
  2. Functions or behavior or methods* or procedures.

Note: * commonly used terms.

We should also know the code syntax rules to create standards in the programming. We will discuss along with the examples.

--

--