Classes and Objects - Python

  • (4.0)
  •   |   553 Ratings

Object oriented programming languages make coding very simple which is why Python tool provides support for classes and objects. In this chapter, we will have a look at how object oriented coding can be done in Python. For those who are new to the OOPs, the following information about the basics will be helpful.


Object: A unique instance of a data structure that’s defined by its class. An object comprises both data members (class variables and instance variables) and methods.

A class is the basic building block for Python. It is created to define all the parameters that are required to create an instance of that class. It logically groups all the functions related to an entity. Technically, a class can be created in any manner, but while programming it is preferable to assign one class to one real world entity.

For example, for a banking software we will create customer class, employee class, loans class, salary, class etc. So, that we can go ahead with programming in a systematic manner.

Related Page: Condition Objects - Python

A class or an instance variable that holds data related to the class and its objects is called a data member. A variable defined inside a method of a class is called an instance variable and a variable defined outside the methods of a class are called class variables. It is also possible to derive one class from the other and the process is called inheritance. Functions defined within a class are also called methods.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

A class is defined in the following manner:

class Class_name:
code(consisting of functions, variables, etc)
The object of a class supports attribute reference and instantiation. 
In case of attribute reference the attributes can be referred in the following manner:
object_name. object_attribute.

Class instantiation on the other hand, requires function notation. You will have to first create an instance:

class_name() creates an instance of the class and it is assigned to local variable object_name

Now, you cannot move further without understanding the importance of ‘ self ’. In python, the first argument of every function in a class always refers to its current instance. ‘ Self ’ is the new object that is created and whose function is called. The value of ‘self ’ attribute is not passed explicitly to a function.

Frequently Asked Python Interview Questions & Answers

So, with this information in mind, let’s analyze the code written above:

firstobj=funcx() # creates object of class funcx()
firstobj.f(7) #calls function f() in the class where x=7, self is current state of
# A value of ‘ 7 ’ is assigned to ‘ x ’ for firstobj

A python class may or may not have to “define a __init__()” function. Sometimes, when there is a need to create an object that are customized to an initial sate , the developer may define a __init__() function in the class.

Instance objects are created by calling the class name and passing the arguments that are required by its __init__ function.

Related Page: Python Classes And Objects

You can access the attributes of an object by using a dot operator and if you want to access a class variable, then you will have to use a dot operator with the class name.

Common ways to access attributes are given below:

-Create an attribute for an object


objectname.attr = value


  • location1.distance=7.0
  • location1.distance

OUTPUT : 7.0

-To check if an attribute exists or not




>>> hasattr(location1, ‘distance’)


-Access an attribute of an object.


getattr(obj, name[, default])


>>> getattr(location1, ‘distance’)

OUTPUT : 7.0

-Set an attribute or create it if it does not exist




  • setattr(location1, ‘distance’,10.1)
  • getattr(location1, ‘distance’)

 OUTPUT : 10.1

–Delete an attribute.


The delattr(obj, name)


  • delattr(location1, ‘distance’)
  • getattr(location1, ‘distance’)

OUTPUT : AttributeError: Point instance has no attribute ‘distance’




Popular Courses in 2018
