字幕列表 影片播放 列印英文字幕 - Now I'm gonna 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 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 the, 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. Et cetera, et cetera. 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, __init_. Again we pass in the instance of the object self, and in this one all we're gonna do is print out your constructed. And here's this code that we've had before and now we have __del and then we pass in self and 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. Um, 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, you know, one and two. And here's an interesting thing, we're actually gonna destroy this variable by throwing away, an no longer points at that object. an's gonna point to 42 so we're gonna sort of overwrite an and put 42 in it, and at that point, Python's 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 wanna be involved at the moment this object is created and I wanna be involved at the moment that this object is destroyed. Now, in this last line, an is no longer a party animal, an 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 gotta be careful if you overwrite something, you could sorta throw the object away. So the constructor is the 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 an 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 gonna put two variables in here, we're gonna have a current score or whatever. And a name. And we're gonna 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 gonna be Sally in this particular thing. And then it'll say, oh, self.name, which'll be Sally, has been constructed. And so then, then we have this, and that object is now constructed in and 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 a... Oops. Another copy of this. And so, this is how it's going to look. Right, as it runs down here. 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 in party and that's all in here, right, all that stuff is in here. And now we say, let's make, and that's gonna have a Sally in there. Alright, Sally in there. And then we're gonna do another constructor and so it's gonna make a whole new thing and it's gonna store that in j, and this one's gonna have Jim in it. s.party then this turns into a one and then we're gonna call j.party, that turns that into a one and then s.party will cause this to be a two. 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, 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.
A2 初級 美國腔 PY4E - Python 對象 (第 14 章 第 3 部分) (PY4E - Python Objects (Chapter 14 Part 3)) 17 1 劉馨兒 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字