Python Tutorials: Learn Python Classes in Detail

Python Tutorials: Learn Python Classes in Detail, plus Encapsulation or Abstraction with Examples
Written by Paayi Tech |19-Oct-2020 | 0 Comments | 935 Views

Classes provide a bundling of the data and functionality in the programming. Creating a new class means that a user is creating a new instance of object, data, and functions and bundling it all together to make a package.

 Classes provide high reuse ability of the code. Not only can we access classes with the same file where they are defined but also other files that make it more useable. In python classes, there are many syntaxes and semantic changes as compared to C++, C#, and java.

 Classes come under the hood of object-oriented programming shortly known as OOP. OOP has three fundamental pillars that are class, object, and methods. We can define all three of these as follows:


What is a Class?

The class describes the object within the class. It describes the data and methods that are bundled.

Instance = Class(arguments)


What is an Object in Class?

An object is an instance of a class; objects have the behaviors of their class. The object is the actual component of programs, while the class specifies how instances are created and how they behave.



The method contains the functionalities that a class can execute on a run time when it is called.

There are three fundamental protocols of OPP, and that is:

  • Encapsulation or Abstraction
  • Polymorphism
  • Inheritance


What is Encapsulation?

Encapsulation or abstraction is the process of data hiding. The abstraction is achieved by the process of encapsulation. OOP heavily relies on the encapsulation of data so that no data can be accessed outside of the class. In C++ and java, there are keywords like public and private data, and in python to there are such keywords to make it more reliable.

 The data is private for outsiders and can only be accessed by the methods of those classes or the class by which it is inherited.

 We can describe encapsulation in a program as following:

class FirstClass(object):

    def getName(self, name): = name

    def printName(self):

        print(f"The name is: {}")


obj1 = FirstClass()




In the above code, we defined a class and named it First-class. It is the convention to write a class name like this (ClassName, MyClass, etc.).

 Then we defined a function that takes a parameter and set the value, and then we again defined a function to print the set value name. Here it is to be noted that I have written self-keyword in every method of the class. The self-keyword shows that this is the part of the class and bind the data with the method.

 To make the instance of the class, we first declare a class in a variable called obj1. Then by this variable, we accessed the getName method and passed the argument, and in the last accessed printName method to print the value.

Some more example of encapsulation is the following:

class calculator(object):

    def getNumbers(self,a,b):

        self.a = a

        self.b = b

    def add(self):



    def sub(self):



    def mult(self):


    def div(self):



obj = calculator()


print ("Addition")


print ("Subtraction")


print ("Multiplication")


print ("Division")













Login/Sign Up


Related Posts

© Copyright 2020, All Rights Reserved.

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.