14.3 - Object Life Cycle

دوره: Using Databases with Python / فصل: Object Oriented Python / درس 5

14.3 - Object Life Cycle

توضیح مختصر

We have terms that we use to describe this, we call it constructor or construction of taking the template which is like a pattern and then building it. Because think of objects as small programs that we're sending stuff into almost like little mini applications that we got an API to talk to. And the key thing to start to realize is that the template is sort of replicated multiple times and then each one can have a different color frosting on it.

  • زمان مطالعه 8 دقیقه
  • سطح خیلی سخت

دانلود اپلیکیشن «زوم»

این درس را می‌توانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید

دانلود اپلیکیشن «زوم»

فایل ویدیویی

متن انگلیسی درس

So now we’re going to talk about how objects are built and then thrown away. And this is, again, something that’s been happening all along. Most of the time we build variables, you say x equals hello world that sort of makes an object. And then when the program ends all the variables get thrown away. Sometimes you can be a little more explicit. We have terms that we use to describe this, we call it constructor or construction of taking the template which is like a pattern and then building it. And then that’s construction and then throwing that away and making the memory available for the rest of the program is the moment of destruction. If you say x equals hello world, you’ve constructed a string and put it into x. If you say x equals 12, you construct at 12 and put that in x, but you’ve also thrown away the hello world that was in there before. So, destructors kind of happen as we reuse variables or as the program ends. And as our objects get a little more complex, the primary purpose the constructor is to set up initial values. And this has to do with the fact that outside the object you just say, make me one of these things, make me party animal. But inside party animal it may need some data and it may need that data be set to zero or something, who knows what. It’s initial, but it’s something that the moment of construction outside, we say, hey, construct me one of these things, and inside, we say, oh, hang on, I’ve got all these little things I’ve got to get right before this object’s actually going to work. Because think of objects as small programs that we’re sending stuff into almost like little mini applications that we got an API to talk to. Except it’s really tiny and it’s just really part of our program. So the way this works is the constructor and destructor are specially named methods. And so in this case, we make PartyAnimal and so far this part’s the same and this part’s the same. But we have an init and passing in self which is that passes the instance of this particular one once it’s constructed. And all we’re going to do is print out that I am constructed, this is the same, and then del is when that thing is thrown away. And so that’s going to say, I am destructed and print out the current value of x for that particular instance. So it comes through here, it has a template. So it’s the same as the one we’re doing and now we’re going to construct. So we’re saying construct me a PartyAnimal, you know the template because I told it to you. We’re not passing any data into constructor. Eventually that will come in as additional parameters. Make me one of these things and then, it’s sitting here. It’s got a little x in it, it’s got this init , it’s got this party and it’s got this del . It’s all, that’s an object now, and that comes into an. And now we are going to call the party method, and that runs and adds 1 to self. But at the moment of construction it prints out this construction message. Then at the moment of calling that method, it says so far 1, and then so far 2. Now this is kind of like inside this an there is an x that’s got 1, and then we change this to 2. We out here have no responsibility for this x. The person who built this had made the x and we just get one for free because it’s part of the template. Now we’re going to destruct this by saying an equals 42. So the way they think about this is an sort of points to this object and then we say an = 42 and it makes a new 42. And then an points to that, and this is thrown away, and then this is thrown away. And the moment, that’s the destructor moment. And so in this line of code it’s going to say, before we throw this away, ah, you’ve got to register destructor, call the destructor. So in this line of code you see the end. The last thing that comes out of this object is I am being destructed and my last value of x was 2. And so then this is thrown away and can be reused. And then 42 is there and then we print out an contains 42. So the end of the program, this doesn’t have a destructor because we don’t have a print statement inside of an integer class because we didn’t make the integer class. But all things are destructed at the end too. And so if there was a thing you could see a destructed message after the end of the program because that’s where all the objects eventually are thrown away and put back so that the memory’s available for other things. So the constructor is the moment we ask to say make me one of these things. And the destructor is whether if you reassign the variable or if the program ends, then all the destructors get called. So the constructor is a special block of statements that are called at the moment of object creation. Now we’re going to talk about what happens when you have more than one instance. We’ve made a template and then we’ve stamped out effectively one cookie and put it in a variable. But now we’re going to do more than one. And the key thing to start to realize is that the template is sort of replicated multiple times and then each one can have a different color frosting on it. And so there are instances of cookies and they can have things like frosting color that changes. And so each of the instances has their own set of variables, so it’s not a class variable, it’s an instance variable. And so here we have our PartyAnimal. And so, it’s going to be pretty much the same as what we’ve been doing. We have a variable x, we have an internal variable named name, and again, this is on the inside, we aren’t worried about this out here in the main program but they’re defining for themselves a structure. And we’re going to have a constructed parameter. So the self is the instance itself which points to the under construction instance and then z is this parameter. So we’re going to take the z that comes in on the parameter and stick in into the name inside the object and then print out that we’ve been constructed. And on and on. So that’s what going on, that’s how the constructor works. We don’t have an destructor in this particular one. And so, as this code runs, this code sort of creates the template, then says make me a PartyAnimal and I want that to be in with the Sally as the parameter. So Sally ends up in here. And then S, our variable S, points at that object. And then we’re going to call, And oh, by the way, x starts out at 0 through the constructor because the constructor sets up x to be 0 and then sets self.name to be whatever this parameter is. And then we call s.party and then party runs and adds 1 to x so that becomes 1. And then comes it back, and now we say let’s make another one. And so let’s construct this one with Jim, and so it makes another template, which means it has an x and a name and some code in it. And the x ends up 0 and the name ends up Jim. And then we take this thing and we assign it into the variable j. So basically j points at this. And we say j.party, and so this is where when we say j.party, it comes up here, and in this case j, self is an alias for j. So its function self.x = self.x + 1. That means this one is going to go to 1, because this is j one and that’s what self means for now. So temporarily self points to j. And then it prints out self.name, party count equals, away we go. And that says this will be Jim. And that will be 1. And now we’ve still got s pointing to this. And so we can call s.party and this time, this is an alias to s. So self points to this guy now. And so self.x = self.x + 1 means this x is going to get incremented. So these are two x’s, two names, but they’re sort of independently stored in each of those objects. And so these are two independent instances, the key here is instances. And that’s because we minted or caused to be constructed two PartyAnimals and then had them in separate variables. And so simultaneously, each one of them has all the internal structure that they need. Now, this isn’t a very useful example. Think of this as, you know, you’ve got a bunch of strings, and one in x, and one in line, and one in z, and one in this, and one in name, and one in email, and they’re all strings with data inside of them. And so the variable names kind of are pointing at the objects that have all the data within them. So up next we’re going to talk about another method of defining the capabilities of objects called inheritance.

مشارکت کنندگان در این صفحه

تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.

🖊 شما نیز می‌توانید برای مشارکت در ترجمه‌ی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.