Python Object and Class

In this article, you will learn about Python object and class. You will also learn how to create a Python class and use it in the main program with objects.

Class and Object: Introduction
How to define a class in Python?
How to create an object in Python?
How to delete objects in Python?
Data hiding in Python
Python built-in class attributes
 python object and class

Python object and class: Introduction

Python is an object oriented programming language. In Python, everything is treated as an object be it functions, modules, data types etc.

A class is simply a blueprint of a data that defines the characteristics and behavior of its data members and member functions and an object is an instance of the class. Creating an object is like defining a variable of class type.

In Object Oriented Programming (OOP) objects are used to store data and functions defined inside a class.

When we define a class we define just a blueprint or we just map data members and member functions. A class doesn’t occupy any physical space in computer memory until an object of that class type is defined. The declaration of class develops a template but data members cannot be manipulated unless the object of its type is created.

How to define a class in Python?

The definition of class in Python starts with the keyword class followed by the name of the class and colon(:).

After the declaration of the class is the body of the class which contains variables, data, and functions. These related data, variables, and functions are bound together in a class and this binding is called Encapsulation.


Let’s define a simple class of cars.

class car:
  "A simple class for cars"
  #Constructor to initialize
  def __init__(self,company,color): = company
    self.color = color

  #function to print car company and color
  def display(self):
    print ('This is a', self.color,

There we have defined a Python class. Let’s explain it in detail.

First is class declaration followed by documentation string. The docstring can be accessed using class_ name.__doc__ i.e. car.__doc__.

After docstring is the __init__ method.

__init__ method

__init__ method is used for the initialization of the data members. This serves as the constructor for the class and as any function, it can take any number of arguments.

The __init__function is automatically invoked when the object of the class is created.

After __init__ method is another function called display to print the car company and color as supplied to the function.

What is that self variable?

Well in every method inside a Python class, self is used as the reference to the object of the class. It is included in a function as a first parameter. When the function defined inside a class is invoked, self refers to the object of the class.

We do not need to pass self as argument while calling the function. Python will automatically include it when the function is called.

How to create an object in Python?

Like we mentioned earlier, a class is just a blueprint which doesn’t occupy any memory until it is instantiated. The instance of the class is called object.

Creating object is same as declaring a variable of class type and we do it simply by calling the class and assigning it to an object (variable).

For example, we have created a class of car and let’s create an object for this class.

>>> #creating an object onj 
>>> obj = car('Ferrari','Red')
>>> #calling the function display of class
>>> obj.display()
This is a Red Ferrari

Note: When we create an object, the __init__ function inside the class is automatically invoked. So, we must supply arguments as the parameters in that function.


How to delete objects in Python?

Python objects are automatically garbage collected when the last reference to the object is gone. This happens automatically.

if you want to destroy the object(which you don’t need to obviously), you can use del statement to delete the object.

>>> #define an object first for the class car
>>> obj = car('BMW','Black')
>>> del obj  #this will delete the object obj

Data hiding in Python

Data hiding is also one of the core features of OOP.

The data members of a class can be declared as private so that they can only be accessed inside a class. This feature of OOP in which we can declare variables and class members as private and hide them making unavailable beyond the class is called data hiding.

A data member can be made private in Python by using two leading underscores. When we don’t make data members private, they can be accessed outside of class. Here is the example to demonstrate it.

class car:
  "A simple class for cars"
  #Constructor to initialize
  def __init__(self,company,color): = company
    self.__color = color  #making color private

  #function to print car company and color
  def display(self):
    print ('This is a', self.__color,

There we have defined the class with private variables as well. Let’s see what happens when we try to access the data members now.

>>> obj = car('BMW','Black')  #creating the object
>>> print (  #company is not private
>>> print (obj.__color)  #color is private
AttributeError: 'car' object has no attribute '__color'

Note: This process of declaring the private variable is called mangling. Use this process only to avoid the name clashes of variables as it has limited support.

As you can see the private data member __color is not available outside the class.

Python built-in class attributes

There are certain built-in class attributes in Python which can be accessed using a dot(.) operator.

Python class attributes with description
Returns a dictionary of classes namespace.
Returns the class documentation string, if defined.
Return the name of the module in which the class is defined.
Return the name of the class.

Let’s take an example to demonstrate how these attributes can be used in the program.

class car:
  "A simple class for cars"
  #Constructor to initialize
  def __init__(self,company,color): = company
    self.color = color  #making color private

  #function to print car company and color
  def display(self):
    print ('This is a', self.color,

print ('car.__name__ = ',car.__name__)
print ('car.__doc__ = ',car.__doc__)
print ('car.__module__ = ',car.__module__)
print ('car.__dict__ = ',car.__dict__)


car.__name__ =  car
car.__doc__ =  A simple class for cars
car.__module__ =  __main__
car.__dict__ =  {'__module__': '__main__', '__doc__': 
'A simple class for cars', '__init__': 
<function car.__init__ at 0x02A5D0C0>,
'display': <function car.display at 0x02EB6E88>, 
'__dict__': <attribute '__dict__' of 'car' objects>, 
'__weakref__': <attribute '__weakref__' of 'car' objects>}

This is all about Python object and class.

In the next tutorial, you will learn about Inheritance in Python.