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:
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
- class MyClass():
- "This is a DocString"
- x = 5
-
- MyClass = MyClass()
- print(MyClass) // output: __main__.MyClass object at 0x00000138B2138508>
- print(MyClass.x) // output: 5
- print(MyClass.__doc__) //output: This is a DocString
Object
An instance is a specific object created from a particular class.
- class MyClass():
- x = 5
- def myFunction():
- print('Hello')
-
- my_object = MyClass() //Creating an object of a class
- print(my_object.x) //output: 5
- print(MyClass.myFunction) //output: <function MyClass.myFunction at 0x0000025AC1D43678>
- 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.
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)
- print(len("Aashina")) //output:7
- print(len([1, 2, 3])) //output:3
Let us see another example of Polymorphism ( User-Defined Polymorphism)
- class Jasmine():
- def color(self):
- print("Jasmine flower comes in white, pink, blue, orange, yellow, purple, and red color")
-
- def scientific_name(self):
- print("Jasminum sambac is scientific name of Jasmine")
-
- def family(self):
- print("Jasmine is from Oleaceae family")
-
- class Lily():
- def color(self):
- print("Lilies commonly grow in white, yellow, pink, red, and orange color.")
-
- def scientific_name(self):
- print("Lilium is scientific name of Lily")
-
- def family(self):
- print("Lily is from Liliaceae family")
-
- obj_jasmine = Jasmine()
- obj_lily = Lily()
- for flower in (obj_jasmine, obj_lily):
- flower.color()
- flower.scientific_name()
- 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)
- class Flower():
- def color(self):
- print("Flowers are found in various colors ")
-
- def scientific_name(self):
- print("These are some Binomial nomenclature for categorize species")
-
- def family(self):
- print("Every flower have a family")
-
- def odour(self):
- print("Every flower have its own odour by which it can be identified")
-
- class Jasmine(Flower):
- def color(self):
- print("Jasmine flower comes in white, pink, blue, orange, yellow, purple, and red color")
-
- def scientific_name(self):
- print("Jasminum sambac is scientific name of Jasmine")
-
- def family(self):
- print("Jasmine is from Oleaceae family")
-
- class Lily(Flower):
- def color(self):
- print("Lilies commonly grow in white, yellow, pink, red, and orange color.")
-
- def scientific_name(self):
- print("Lilium is scientific name of Lily")
-
- def family(self):
- print("Lily is from Liliaceae family")
-
- obj_Flower = Flower()
- obj_jasmine = Jasmine()
- obj_lily = Lily()
-
- obj_Flower.color()
- obj_Flower.scientific_name()
- obj_Flower.family()
- obj_Flower.odour()
-
- obj_jasmine.color()
- obj_jasmine.scientific_name()
- obj_jasmine.family()
- obj_jasmine.odour()
-
- obj_lily.color()
- obj_lily.scientific_name()
- obj_lily.family()
- 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.
- class myClass(object):
- def __init__(self):
- self.a = 123 // private variable
- self._b = 123
- self.__c = 123 //private variable, double underscore
-
- obj = myClass()
- print(obj.a)
- print(obj._b)
- print(obj.__c)
Sample output
Private variables are intended to be changed using getter and setter methods. These provide indirect access to them.
- class myClass(object):
- def __init__(self):
- self.__version = 22
-
- def getVersion(self):
- print(self.__version)
-
- def setVersion(self, version):
- self.__version = version
-
- obj = myClass()
- obj.getVersion()
- obj.setVersion(23)
- obj.getVersion()
- print(obj.__version)
Sample output
Summary
In the next, we will learn to use Python Class.