Python Object & Class


Basic Features of OOP(Object Oriented Programming)

1. Object:

  • Object is nothing but collection of data members and method in single unit called object.
  • Object is a physical entity.An object might be anything like person,vehicle,things etc.To Confirm an entity is an object we can cosider three components .

They are below,

i. state:

It indicates the characteristics of real world entity and also it refers to the present condition or state of the entity.Ex:color,height,weight,name etc.

ii. Behaviour:

It is nothing action/reflection of an object. Ex:Running,idle,walking etc.

iii. Identity:

It is an unique identification of the real world entity.It will differentiate to one to entity to another entity Ex:ID,Reg No,DL No Etc.

2. Class:

Class in nothing but collection of objects and also we can say class is blueprint where behaviour and attributes defined.Considered Animal is class and Dog, Monkey,Donkey, Cat etc all are the object for the class Animal.

                                    Wild Animal









3. Encapsulation:

Wrapping of data and method,method should work on that data is called Encapsulation,i.e capsules

4. Abstraction:

Collection of essential data to our application and neglate the irrilavant data to our application

5. Polymorphism:

Single interface with multiple implememtation we can called polymorphism.We can achieve the poly morphism in two ways they are.

1. Function Oveloading
2. Operator Overloading

6. Inheritance:

Inheritance is the concept ,here we can create the new class by using alreay existing class.Newly created class we can call derive class or child class,already existing class we can call it as a base class or  parent class.Python supports Simple,Multilevel and multiple

Access Specifiers:

There is no access specifiers in python like c,c++(public,private,protected),but we can achieve this mechanism in deifferent ways now we can see that

 1. public:

  • All data memebers and methods in python by default public access specifiers,Means we can acces data memebers and methods entire the source code
  • For public  access specifiers we can’t give any type special syntax it is normal variable and methods 


2. protected:

  • protected data members and methods are accessable with in base class and derive class only.
  • To identify this knid of  access specifiers we can use the data member or method can start with single underscore “_” this we can call it as protected data members.But keep it in mind protected we can use only when we have subclasses in sorce code. 


3. private:

  • private data members and methods are not accesible anywhere except that class which is having data members and methods,but it can be inhereited the sub classes but can’t be accesasable.
  • To identify this knid of  access specifiers we can use the data member or method can start with double underscore “__” this we can call it as private access specifier. 


self in python:

  • self is not predefined keyword in python programming language.But mostly for understanding people are using the self keyword in python for object oriented programming language.
  • When ever we are going to implement the class in python especially in methods we are using self keyword as an argument ,because of when called class method by using obect of that class then interpreter intenally passed object as an argument to the method,so most people in class method they can consider first arument as an object argument,we can give any type of name not only self.

__init__() method (constructor):

This is the  method called automatically once the object has been created with out invoking any object and it will call only one time entire life time of the program.Mostly this method is used to  intialize data members with some default data,If require to do some functionality is need to after object created immediately.

Class Variables:

The variables which are we declare after immediately the class created we can call it as  class variables .Class variables are we can access entire the application,the data is sharable by multiple objects.


Object Variables:

The variables which are dedicated for individual objects that variable we can called as object variables.



How to create plain class and methods




Creating class with having class variables




1. The above example i have created the with class variable and object method variables
2. I have created two objects for that class names are s1,s2
3. I have given the diferent details for both objects,  i have given database is “student”,Now for both object having “student”
4. Now i have changed the database with “new_student”,after that i have display the details,there if we observe it has been reflected in both objects


Constructor or __init__ Method:

  • constructos are the special memmber function it will get invoke automatically at the time of creation of an object,it will intialize the data members of that object.In python we can create the constructors with the help of __new__ () method and __init__() method.
  • Here __new__() will create the object and __init__() will initialize the object.

Destuctor or __del__ Method:

destructos are the special memmber function it will get invoke automatically at the time of creation of destroying the object,it will un-intialize the data members of that object




Function Overloading:

Having the same function name with multiple implememtation we can call it as a function overloading,function overloading will happen number of arguments,type of arguments and order of arguments.Function oveloading is not happen with respect to return type.



Example 5:

 Operator Overloading:

  • The Operator which are available in python working fine with predefined data types,but theya are not compatible with user defined data types,so to make working fine with them we are going to take the help of operator overloading
  • Below is the example program we are going to overloading the ‘+’ operator . 



suppose consider we not going to use any operator overloading concept to add two objects then what will happen

step 1:

 class addOvrld:

    def __init__(self, a = 0, b = 0):

        self.a = a

        self.b = b

consider above i have created the class with constructor,then below i have created two objects for that class

obj1 = addOvrld(2,3)

obj2 = addOvrld(2,2)

and then add the two objects

print(“Addition of Two Objects is:”,obj1 + obj2)

 then i can get below error

TypeError: unsupported operand type(s) for +: ‘addOvrld’ and ‘addOvrld’

 so as per this experiment we have understand we can’t add two objects directly it will give unexpected behaviour

Why __str__() method:

If we observe above code we have used __str__() method ,because if we try to acceess the object data it will not give proper expected output.To get expected output we are going to use inbuit __str__method

obj1 = addOvrld(2,3)

suppose if we try to print the obj1 without using __str__ () method,then we will get below output

<__main__.addOvrld object at 0xb720508c>

suppose if we try to print the obj1 by using __str__ () method inprogram,then we will get below output


How operator overload can work internally:

Once we have perform the addition operation between two objects,then it will call __add__() method,but intenally it will invoke the __add__ method like obj1.add(obj2)

Few more default function for operator overloading

Operator        /   Operation  Internatal implementation
Addition            /      obj1+obj2     




Substraction      /     obj1-obj2   




Multiplication     /    obj1*obj2  




Division              /      obj1/obj2 




Modulus               /     obj1%obj2 




Less Than           /       obj1<obj2   




Equal to            /         obj1=obj2  




Not Equal to       /       obj1!=obj2 




Greater Than      /          obj1>obj2  




Less Than equal to   /    obj1<=obj2 




Greater Than equal to  / obj1>=obj2    




Bitwise AND                /     obj1&obj2     




Bitwise OR                   /          obj1 | obj2    obj1.__or__(obj2)
Bitwise XOR                    /      obj1^obj2    obj1.__xor__(obj2)
Bitwise NOT                   /        ~obj1    obj1.__invert__(obj2)
Bitwise Right Shift          /       obj1<<obj2  obj1.__rshift__(obj2)
Bitwise Left Shift             /      obj1>>obj2     obj1.__lshift__(obj2)


Share this website !!!
Facebook Twitter Linkedin