Python Tutorial for Beginners 16 - Class Constructors (__init__) and Destructor (__del__)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in the last video I have shown you how to use classes in Python now in this video I am going to show you how you can use constructors and destructors with classes in Python this constructor and destructor are also called initialization and deletion member functions of the class so first of all let's see how we can use constructor in Python in the last video I have given you a small little example of class and how to create a class and we have created a person class and we have created two member function one member function to set the values and other member function to print the name right so first member function we named it as set full name and in this function we have we were passing two arguments first name and last name and this printful named member function we were using to print the name of the person and then I have shown you how you can create an instance or object of a class like this and how to use these member function like this using the instance of the glass now I'm going to take the same example to show you how you can use constructor with this same class so first of all to define a constructor you just use normal function definition with d EF and then you give double underscore in it then double underscore and give the bracket and give colon here and in here as any other member function you can perform any task so for example I want to print something that our class is created for examples so I'm just printing a class is created not like this but class is created right so first of all what is the use of this constructor generally in programming languages constructors are used to initialize the value okay so whenever you create the instance of a class so we are creating the instance of a class here as soon as you create the instance of a class this constructor is called immediately so you don't need to explicitly call the constructor of a class you may call it or you may not call it but don't forget to give the self argument as I am showing you in the last video that you need to use this keyword self with every member function of a class and this indicates to the instance of a class so program a person name will be replaced by self here in the last video I have shown you so as soon as I create an instance of a class this init member function will be automatically called so you don't need to explicitly call this member function so I am going to run this save run and you can see here it gives us the result our class is created even though we haven't called our init member function explicitly it's printing first this our class is created that means as soon as you create this instance of a class this member function is called right so always remember the constructor definition is reserved here with double underscore in it and this also ends with double underscore so this is the reserved keyword in Python 2 called constructor you cannot give any definition or any name to the constructor it's reserved which is this one in a same way we can use destructor to destroy the instance of a class okay so and destructors are also called whenever our instance is out of scope so let me show you how you can call this Tuerto first so destructor also you can just define it as a normal function with to underscore de l and this is two underscores and then just call self here keyword and then keep colon here and then you can just write the functionality of the code for example I have or will print I can print our class instance is destroyed so our class object is destroyed and this is also reserved in Python with double underscore de l and this also ends with double underscore and this function is also automatically called whenever your class or in class instance goes out of scope out of scope me means that this class instance is no longer used so in here after this code our class instance scope is completed because this is no longer used in further code because there is no code obviously right then whenever this class or person name goes out of scope this deletion member function will be called automatically and it just basically the use of this destructor deletion member function is to a garbage collection kind of use so it just collect the garbage or all the resources and destroy all the resources which are used by this instance which is person name so I will just save my program and run it and you can see here whenever I just created this it's not printing this deletion member message here but whenever you can you use the instance of a class dot underscore underscore deleting so underscore deletion member and then whenever you run this this is printed a class object is destroyed so whenever you use this deletion member function it destroys all the resources which are used by your class instance okay so this is the use of this destroyer or destructor now it's not a you know compulsory that you cannot pass any argument with this initialization you can pass argument for example I can pass for example I can pass any argument for example I want to pass I D of the person I am I'm using or I want to print the name of so I want to initialize the ID of a person like this I can just pass this argument with my initialization member function and then I can just use self dot ID is equal to ID and then I can call this ID in my destructor also so for example this structure can also take some arguments and we can call this self-taught ID for example I will print this ID here and then I can just print the message right so it's not necessary that our initialization or our constructor has to has to initialize with no argument it can take any number of argument as an initialization but whenever you give this argument in the initialization here then whenever you create the instance you need to pass the that argument here so we are creating the instance here and because the initialization takes this one argument which is ID we are passing 5 as an ID here in the initialization ok so this is how you can initialize it and now whenever you run your program once again you can see here our class is created once again and as soon as this class is created we are setting the ID of a person as we pass in the constructor which is here and then self dot ID is set here and whenever we call the destructor we are once again calling the ID of the person which is this one and then printing the message this one right so in this way you can use a constructor and destructor or initializer or and deletion in Python and I hope you have enjoyed this video please rate comment and subscribe and bye for now
Info
Channel: ProgrammingKnowledge
Views: 134,674
Rating: 4.6114287 out of 5
Keywords: Architecture (Industry), Construction (Industry), Python (Programming Language), Class, Self (Programming Language), Init, Python 3.6, Python, Install Python, Python 3.x.x, Programming Language (Software Genre), Python Tutorial, Python for Beginners, Python course, python tutorial, Online Course, Python Guru, Learn Python, python 3.7, Python 3, Python Class, Python Class examples, python 3 class, __init__, self, Python __init__
Id: NSssW0duUkQ
Channel Id: undefined
Length: 11min 6sec (666 seconds)
Published: Wed Oct 01 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.