Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Constructor and Destructor in Python

 

In this lecture we will see that -

What is Constructer and Destructor ?

What is the life cycle of object ?

How to implement class and objects in Python ?

 

Constructors in Python

 

Class functions that begins with double underscore (__) are called special functions as their special meaning.

 

_init_(self) function - this special function gets called whenever a new object of the class is instantiated.

 

Life cycle of object in Python

 

Step 1 : Definition

Python defines its classes with keyword 'class' which is defined in Python interpretor.

 

Step 2 : Initialization

When an instance of the class is created, _init_ method defined in the class is called. It initializes the attributes for newly created class instance. A namespace is also allocated for object's instance variables.

 

Step 3 : Access and Manipulation

Methods defined in a class can be used for accessing and modifying the state of an object. This are accessors and manipulators respectively. A class instance is used to call this methods.

 

Step 4 : Destruction

Every object that's created needs to be destroyed. This is done with Python garbage collection (that is reference counting).

 

Example :

class Student:

"To get Student Information..."

SchoolName="K.S School"

id=12

name="Parth"

email="parth@gmail.com"

def_init_(self,id,name,email):

self.id=id

self.name=name

self.email=email

print ("Value set...")

def displayStudentData(self):

print("Id : ",self.id)

print("Name : ",self.name)

print("Email: ",self.email)

print()

ob=Student(1,"Nirmal","nirmal@gmail.com")

ob.displayStudentData

print(Student._doc_)

print(Student.id)

print(Student.name)

print(Student.email)

O/P :

Value set...

Id : 1

Name : Nirmal

Email : nirmal@gmail.com

To get Student Information...

12

Parth

parth@gmail.com

FAQs

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere