Now, we're going to talk a little bit about object lifecycle, and what we mean by object lifecycle is the act of creating and destroying these objects. Now I've been using this term constructor already and so when we declare a variable, whether it's a string or a dictionary or a party animal, whether we create them and then they're discarded and there's all this dynamic memory that comes and goes. And we as the writers of objects have the ability to insert ourselves at the moment of object creation and at the moment of object destruction. And we make special functions that we call the constructor, the object constructor, or the class constructor, and the destructor. And we don't actually explicitly call them, they're called automatically by Python on our behalf. And so the constructor is much more commonly used. It's used to set up any initial values of variables if necessary, etc. Destructors, we'll cover them but they're used very rarely. So here's a bit of code that we've got. It's our PartyAnimal and a lot of it is the same as what we've been doing so far. So we have this variable x and the constructor has a special name, underscore underscore init underscore. Again, we pass in the instance of the object self and in this one all we're going to do is print out that you're constructed. And here's this code that we've had before and now we have underscore underscore del, and then we pass in self and then we'll just print out that we're being destructed and what the current value of x is for that particular instance. So let's go ahead and run this. And so again, this doesn't really do any code up to here, that just defines PartyAnimal, but this is the constructing of it. And basically that says, Oh. And it really kind of creates these variables and then it also runs the constructor. And so in this case, this line right here is causing the "I am constructed" message to come out. Then we do an.party, an.party, and that says 1 and 2. And here's an interesting thing, we're actually going to destroy this variable by throwing away, a n no longer points at that object, a n is going to point to 42, so we're going to sort of overwrite a n and put 42 in it and at that point Python is like, Oh, this whole little object that I just created somewhere it's out here, it's vaporizing it and throwing it away and so before this line completes, it actually calls our destructor on our behalf, and so that message comes out. So we are allowed as the builder of these objects to add these little chunks of code that says, "I want to be involved at the moment this object is created and I want to be involved at the moment that this object is destroyed." Now, in this last line, a n is no longer a PartyAnimal, a n is now an integer. It's got a 42 in it. It's gone. It's been created, it was used, and then it was destroyed, okay? So you've got to be careful if you overwrite something you sort of throw the object away. So the constructor is a special block of code that's called when the object is created to set the object up. So we can create lots of instances. Everything we've done so far is we make a class and then we create one instance, one object. And each of these objects ends up being stored in its own variable. We have a variable a n and we've been using it. But the more interesting thing begins to happen when we have multiple instances of the same class sitting in different variables, and it has its own copy of the instance variables. So let's take a look at this. So this code here, I've taken out the destructor and it shows a little bit more information. So now we're going to put two variables in here, we're going to have a current score or whatever and a name and we're going to start it out as blank. And this time we're going to add a parameter onto the constructor. And so the self comes in sort of automatically as the object is being constructed, but if we put a parameter on the constructor call, which is this PartyAnimal call, then this comes in as the z variable. And so self is the object itself and z, this first parameter, is whatever parameter we put here. Everything we've done so far has no parameter here, but now we have a parameter here. And then that means that when we call this constructor, this line of code comes and then name is no longer blank, name is going to be Sally in this particular thing. Then it'll say, Oh, self.name, which will be Sally, has been constructed. And that object is now constructed and then we put it in the variable s. And then we call the party method on that and we construct a different one. And so this time it calls and z is Jim and we basically have another copy of this. And so this is how it's going to look, right? As it runs down here, when this is called, it makes one instance and stores that in the variable s and there's a variable x in there, there's a name in there, there's an init method and party, and that's all in here, all that stuff is in here. And now we say, let's make, and that's going to have Sally in there. And then we're going to do another constructor and so it's going to make a whole new thing and it's going to store that in j and this one's going to have Jim in it. s.party, then this turns into a 1 and then we're going to call j.party that turns that into a 1 and then s.party will cause this to be a 2, okay? And so what happens is we have now two objects, one in the variable s and one in the variable j, and they have separate copies of their instance variables. These are the instance variables or the object fields or whatever, but they're the variables. But the key is that every time we do a new construction, it duplicates this and there's another copy of it. So there's an x within s. So s.x is this variable and j.x is that variable. Okay? So the next thing we'll talk about is inheritance and that's the idea of taking one class and extending it to make something new. [MUSIC]