Friends here our concept is
'destructor' of a Class. See already we know about
'constructor'. A 'constructor' is a special method and that is
called by default
whenever you create an object from a class.
So first, let me show you this. Already we know this.
So let me define a class called 'Person',
[no audio]
then in that I am defining a method called 'init'. You know
that is nothing but 'constructor' of your class, then 'print(
"an object has been created".)'
You know this method will execute by default whenever you
create an object.
from this class. Right. See that. I am creating an object called
'per1', then let me write simply,
[no audio]
and let me run this and see the result. "an object has been created".
That's it.
And because of this advantage, right, we can define
variables or we can call some methods by default in
this initialization method.
Let's say for my 'per1'
I need to assign name and age.
So for that, you don't need to write a separate method. Suppose
if you don't have 'constructor' then suppose name and age,
let me write 'name_age'. Then for this you have to pass
your 'name' and 'age', right. And then you have to write
'self.name=' some 'name', then 'self.age='
some 'age', then simply write 'return None'.
And to assign your 'name' and 'age' you need to call
from 'per1' 'name_age' method, like
in this way. So after creation of an object, 'per1.name_age', then
you have to pass,
suppose some name,
[no audio]
then let's say some age, in this way.
[no audio]
Right, but because of this advantage, you know by default
initialization method is calling, that's why what I will do
is instead of defining your name and age for your object,
I will write the code here itself and I will delete this
method. Now, I reduced one method to define my age, and I mean
for my 'per1', or for my object, name and age.
Right. Yeah, fine.
[no audio]
Now this initialization method is requiring two variables - name
and age. Now, let me take them as arguments.
[no audio]
And now your 'self' method requires two arguments, right. See
that, in the output error, name and age, 'missing 2 positional
required arguments: 'name' and 'age'.'
That's why while calling,
I mean while creating an object, here itself you have to
pass the arguments which are required for your
[no audio]
initialization method or 'constructor' method. That's it.
[no audio]
Right. That's fine.
Now, just assumed that
you've done your work with your objects,
[no audio]
then I don't want to waste my RAM memory,
then I need to delete my objects from RAM.
Then at the end, suppose you have to write deletion 'per1'
object, or if you create any number of objects, that many
number of objects
you have to delete it.
Right. That's fine.
That's fine. You can delete it manually or explicitly by writing 'del', your
object name. But in Python you don't need to delete objects.
[no audio]
Right. See that first thing, 'destructor'.
Destructor is a method first of all, and that method will
call when an object gets destroyed.
But we don't need to delete an object. Python is having automatic
garbage collection or garbage collector, that will manage
memory automatically. See that. But to understand that whether it
is deleting or not,
there is a predefined 'destructor' method that is called
'__del'.
And be clear, you don't need to write this deletion method,
and don't need to use in your codes, but for interview point
of view you have to know what is the use of deletion method.
[no audio]
First let me write my code, "object has been deleted".
[no audio]
Let me write 'return', simply 'None'. Now see the output. Because
of this object, I mean automatically this initialization method will call,
and because of automatic garbage collector of your Python
once if you've done your work, it will automatically delete.
Whenever your 'per1' object is deleting from your memory,
at that time just it will call automatically deletion method
if you have inside of your code. If you don't have, no problem.
And in this method we have this information that's why it is
printing. So guys
finally you have to know one thing, 'constructor' method will
call by default whenever if you create an object, and deletion
method or 'destructor' method will call by default whenever
an object has been deleted from your memory by your Python,
but not by you. Automatically you'll delete an object. At that time
just to know that whether it is deleting,
or not we are using 'deletion' method. That's it.
Okay. But while writing your codes, you don't need to use
this 'deletion' method. But if you use it's good. Right. But
for interview point of view, it is important simply. Right.
Okay guys, thank you for watching this simple video.
[no audio]