Python Classes and Objects
Python Classes
In object-oriented programming, a class encapsulates real-life complexity and makes it simple for reuse, and a programmer creates objects based on these classes. When you create an object from a class, it automatically inherits the traits of the class, and new methods and behaviours can be added to the object.
A Python class is a collection of variables(attributes) and methods(functions) that defines the working of an Object. A class does not take up memory space. It is just a description of the object. Python object and class model supports advanced notions such as polymorphism, operator overloading, and multiple inheritances.
For example
We can define 'Human' as a Class. This class will have attributes like ‘Age', 'Color', 'Job' and 'sex'. It will also have a method getAge().
We use the keyword class to define a class in Python.
from datetime import date
class Human:
age = 0
Color = "White"
Job = "Actor"
sex = "Male"
def __init__(self, age, color, job, sex):
self.age = age
self.Color = color
self.Job = job
self.sex = sex
def getAge(self):
return self.age
Methods
The functions that are defined inside of a Python class are known as Methods.
For example
__init__() method
The __init__() method is used to initialize the data members when a new object is created. Generally, we provide an initial value to the member variables inside the __init__() method. It is the first method that is called when a new object of a class is created.
For example
from datetime import date
class Human:
age = 0
Color = "White"
Job = "Actor"
sex = "Male"
def __init__(self, age, color, job, sex): #initialize the object
self.age = age
self.Color = color
self.Job = job
self.sex = sex
def getAge(self):
return self.age
self keyword that is specified as an argument in the __init()__ is a reference to the object being created. Every method of the class must have self as an argument.
Python Objects
Objects are implementations or instances of a Python class. These objects take up space in the memory.
For Example
'Deepak' and 'Ruby' are two objects of the 'Human' Class.
from datetime import date
class Human:
age = 0
Color = "White"
Job = "Actor"
sex = "Male"
def __init__(self, age, color, job, sex): #initialize the object
self.age = age
self.Color = color
self.Job = job
self.sex = sex
def getAge(self):
return self.age
Deepak = Human(20, "White", "Teacher", "Male")
Swati = Human(21, "White", "Actor", "Female")
Output:
As you can see, we used the . operator to access the member variable sex.
Python Inheritance
Just like a human child inherits from its parents, One Python class can inherit from another. The class that inherits is called Child Class, while the other is called Parent Class. Inheritance allows the programmer to reuse already implemented functionality.
In Python, we can specify the parent class of the child class like this
For Example
class Animal: #Parent Class
age = 0
color = "White"
sex = "Male"
def __init__(self, age, color, sex): #initialize the object
self.age = age
self.Color = color
self.sex = sex
class Human(Animal): #Child Class
Job = "Actor"
def __init__(self, age, color, job, sex): #initialize the object
self.age = age
self.Color = color
self.Job = job
self.sex = sex
def getAge(self):
return self.age
In this example code, Class Human inherits from Class Animal. It inherits the variables age, color and sex from the parent. The variable “Job” and method getAge() are their own original members.
Python Polymorphism
Poly means Many, and Morph means Forms. Thus, Polymorphism means something having many forms. OOP in Python allows the programmer to create polymorphic interfaces.
For example
With OOP, the programmer can create a single function that can handle multiple types of input data. This is called Function Overloading.
class Animal: #Parent Class
age = 0
color = "White"
sex = "Male"
def tryToFly(self):
pass
class Ostrich(Animal): #child Class
def tryToFly(self):
print("Ostrich was not able to Fly")
class Sparrow(Animal): #child class
def tryToFly(self):
print("Sparrow was able to Fly")
def flyTest(Animal):
Animal.tryToFly()
ostrich = Ostrich()
sparrow = Sparrow()
flyTest(ostrich)
flyTest(sparrow)
Output:
Python Encapsulation
Encapsulation is the process of binding data and code together so that direct modification or access to data is impossible. The programmer can decide whether a variable or method will be publicly accessible or not. This can be done using access modifiers.
These are the available access modifiers in Python:
Access Modifier | Description |
---|---|
Public | Can be accessed from anywhere in the program |
Protected | Can be accessed only by the classes that inherit from the current class |
Private | Can only be accessed from inside the current class |
In Python, all the members of a class are public by default. To make a member protected, we need to prefix a '_' before the member's identifier or variable name.
For example
class Animal: #Parent Class
_age = 0 #protected Variable
color = "White"
sex = "Male"
To make a member private, we need to prefix a '__' before the member's identifier or variable name.
For example
class Animal: #Parent Class
__age = 0 #private Variable
__color = "White"
sex = "Male"
def __init__(self, age, color, sex): #initialize the object
self.age = age
self.Color = color
self.sex = sex
self.wings = wings