Python Object Oriented Programming Concepts

Introduction

 
In this chapter, we will have a look at the OOPs concepts that are supported by Python.
 

Object 

 
Languages that use objects in programming are called Object-Oriented Programming. Everything in Python is an Object.
 
Object is an abstract data type. An object has two characteristics:
  • Attributes
  • Behavior
Let us take an example, Child is an object. Its characteristics are Name, Age, Blood Group, Caste, etc. Attributes are: Crying, Dancing, Playing, Studying, etc. (Behaviour)
 

Oops Concepts in Python

Class

 
The class is a blueprint that defines the nature of a future object. A class is a collection of objects
  1. class MyClass(): # Creating a class      
  2.   "This is a DocString"    
  3.    x = 5    
  4.     
  5. MyClass = MyClass()    
  6. print(MyClass)  // output: __main__.MyClass object at 0x00000138B2138508>      
  7. print(MyClass.x)  // output: 5      
  8. print(MyClass.__doc__)  //output: This is a DocString   

Object

 
An instance is a specific object created from a particular class.
  1. class MyClass():  
  2.    x = 5  
  3.    def myFunction():  
  4.      print('Hello')  
  5.   
  6. my_object = MyClass() //Creating an object of a class    
  7. print(my_object.x) //output: 5    
  8. print(MyClass.myFunction) //output: <function MyClass.myFunction at 0x0000025AC1D43678>    
  9. print(my_object.myFunction) //output: <bound method MyClass.myFunction of <__main__.MyClass object at 0x000001B69C068488>

Relationship between a Class and an Object

 
In this example, the Class is Human, i.e a logical entity with some attributes and methods for example hair, nose, legs, arms, etc. And; Object is an entity that has state and behavior. So here, In the class Humans, we have Objects like man, woman, and child, they may have some state and behavior such as sleeping, eating, etc. 
 

Polymorphism

 
Poly means 'many' and  Morphism means 'forms'. In Polymorphism, the functions are of the same name but their functionalities are different.
 
For Example: (In-Built Polymorphism)
  1. print(len("Aashina")) //output:7    
  2. print(len([123])) //output:3  
Let us see another example of Polymorphism ( User-Defined Polymorphism)
  1. class Jasmine():  
  2.   def color(self):  
  3.      print("Jasmine flower comes in white, pink, blue, orange, yellow, purple, and red color")  
  4.   
  5. def scientific_name(self):  
  6.   print("Jasminum sambac is scientific name of Jasmine")  
  7.   
  8. def family(self):  
  9.   print("Jasmine is from Oleaceae family")  
  10.   
  11. class Lily():  
  12.   def color(self):  
  13.      print("Lilies commonly grow in white, yellow, pink, red, and orange color.")  
  14.   
  15. def scientific_name(self):  
  16.   print("Lilium is scientific name of Lily")  
  17.   
  18. def family(self):  
  19.   print("Lily is from Liliaceae family")  
  20.   
  21. obj_jasmine = Jasmine()  
  22. obj_lily = Lily()  
  23. for flower in (obj_jasmine, obj_lily):  
  24.   flower.color()  
  25. flower.scientific_name()  
  26. flower.family() 
Sample Output
 
 

Inheritance

 
By using inheritance, we can create a class that uses all the properties and behavior of another class. In Inheritance we have a parent and a child class, as in real life a child inherits the characteristics of his/her parents, likewise, child class has properties of the parent class.
 
Let us take the previous example again: (Polymorphism with Inheritance)
  1. class Flower():  
  2.   def color(self):  
  3.      print("Flowers are found in various colors ")  
  4.   
  5. def scientific_name(self):  
  6.   print("These are some Binomial nomenclature for categorize species")  
  7.   
  8. def family(self):  
  9.   print("Every flower have a family")  
  10.   
  11. def odour(self):  
  12.   print("Every flower have its own odour by which it can be identified")  
  13.   
  14. class Jasmine(Flower):  
  15.   def color(self):  
  16.      print("Jasmine flower comes in white, pink, blue, orange, yellow, purple, and red color")  
  17.   
  18. def scientific_name(self):  
  19.   print("Jasminum sambac is scientific name of Jasmine")  
  20.   
  21. def family(self):  
  22.   print("Jasmine is from Oleaceae family")  
  23.   
  24. class Lily(Flower):  
  25.   def color(self):  
  26.      print("Lilies commonly grow in white, yellow, pink, red, and orange color.")  
  27.   
  28. def scientific_name(self):  
  29.   print("Lilium is scientific name of Lily")  
  30.   
  31. def family(self):  
  32.   print("Lily is from Liliaceae family")  
  33.   
  34. obj_Flower = Flower()  
  35. obj_jasmine = Jasmine()  
  36. obj_lily = Lily()  
  37.   
  38. obj_Flower.color()  
  39. obj_Flower.scientific_name()  
  40. obj_Flower.family()  
  41. obj_Flower.odour()  
  42.   
  43. obj_jasmine.color()  
  44. obj_jasmine.scientific_name()  
  45. obj_jasmine.family()  
  46. obj_jasmine.odour()  
  47.   
  48. obj_lily.color()  
  49. obj_lily.scientific_name()  
  50. obj_lily.family()  
  51. obj_lily.odour() 
Sample Output
 
 

Encapsulation

 
Encapsulation refers to restricting access. This is how we can wrap up and remove unnecessary data. So basically, Encapsulation is the process of using private variables within classes to prevent unintentional modification of data.
 
 
A single underscore: Private variable, it should not be accessed directly. But nothing stops you from doing that (except convention).
 
A double underscore: Private variable, harder to access but still possible.
 
Both are still accessible: Python has private variables by convention.
  1. class myClass(object):  
  2.   def __init__(self):  
  3.      self.a = 123 // private variable    
  4. self._b = 123  
  5. self.__c = 123 //private variable, double underscore    
  6.   
  7. obj = myClass()  
  8. print(obj.a)  
  9. print(obj._b)  
  10. print(obj.__c)  
Sample output
 
 
Private variables are intended to be changed using getter and setter methods. These provide indirect access to them. 
  1. class myClass(object):  
  2.   def __init__(self):  
  3.      self.__version = 22  
  4.   
  5. def getVersion(self):  
  6.   print(self.__version)  
  7.   
  8. def setVersion(self, version):  
  9.   self.__version = version  
  10.   
  11. obj = myClass()  
  12. obj.getVersion()  
  13. obj.setVersion(23)  
  14. obj.getVersion()  
  15. print(obj.__version) 
Sample output
 
 

Summary

 
In the next, we will learn to use Python Class.
Author
Aashina Arora
335 5.3k 540.2k
Next » Python Class