14.2 - Our First Class and Object

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

14.2 - Our First Class and Object

توضیح مختصر

But we're expecting you to be increasingly good at using classes that are built into Python. It's just kind of a, so far, it's like a clever way to hide or embed a function inside this template. So up next, we're going to look into a little more detail of this notion of constructing and destructing multiple instances of objects.

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

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

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

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

فایل ویدیویی

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

So now we’re going to take a look at building a bit of Python, and building a class in Python. Not, again, so much as, because I want you to write a bunch of code. But instead, I want you to imagine as you’re using classes, that someone had to write this code. And so, we’re not going to make you write a bunch of classes. But we’re expecting you to be increasingly good at using classes that are built into Python. Or come from libraries that you might import. And so, here’s some code. So class is kind of like a function. There’s a new keyword called class, and there is the name of the class, class PartyAnimal. And so that’s the name of the class, a colon, and then an indented block. And so, it’s got sort of a block of text that makes up the class. And as I mentioned, every class has some data associated with the class, and some code associated with the class. And so here in this class, we just have some variables, x = 0. That’s an attribute of, all PartyAnimals will all have a variable named x in them. And there’s a little function. So this is now a method, so it has a bit of code, it’s a method. And then when this is done, it doesn’t actually run any code. But what happens then is, we have a template called PartyAnimal. And this syntax here, that basically says make me a PartyAnimal, it’s the same as saying x = list(). It says mint me a new list, there’s a template for a list here. And then give it to me, give me that empty list back in the variable x. So that’s what’s happening here, mint me a new PartyAnimal, based on the template that’s up here. And then once that empty, or fresh, PartyAnimal is done, then give it to me back in the variable an. And so this, then, is that object. So that is our PartyAnimal object. Using the PartyAnimal template makes a PartyAnimal object, which ends up in the variable an. Or, the PartyAnimal instance of the PartyAnimal class. And then we make a call by taking the name of the object, dot, dot is the operator. And then the method within it. So that basically calls this code, okay? And then it calls it again, it calls it again, it calls it again, that you can put parameters here. And you’ll notice that this party has no parameters, and this one has one parameter. And basically, you can think of this as, it’s kind of like saying, go into PartyAnimal. Call the party() function within that, and then pass, as the first parameter, this variable. So that you can almost think of this syntax as this is like a contraction for this. Which says go in, find this PartyAnimal, and then pass an, an the variable comes in as self. Now you don’t have to name this self, but you can name it self. And nearly everyone who writes Python object oriented tends to call this self. And self says, self.x = self.x +1. Well, that’s kind of like saying an.x = an.x+1. Because we’re calling it in the context of an. And so, when we see more than one instance, we’ll see how this all works. And so, this is basically the object. We have the definition, we have the minting, the templating, the making of the cookie. And then we have the use of that object. And so if we trace through the code that’s going to happen here. There is nothing, there is just the template, and then we mint this. And then in the minting, it runs through this and defines these things. And then x ends up with the variable value 0. And then, this whole object, this whole box is the object because it has a variable, an attribute x, and a bit of code called party(). And then we take this. It gets returned, and then it comes back as an. And so an sort of points at this particular object, when this line of code is done. So make the thing from the template, and then assign it to an, so an points to this. Then what happens is we hit this. And so that calls, that goes up and calls this party, except that an is, self is an alias to an. So temporarily, self is also pointing to this. And then it says self.x=self.x+1. So that pulls the 0 out of here, adds 1 to it, becomes 1. Stores it back in, which makes this be a 1. And then it also has a print(“So far”,self.x), and then that prints out this print. Then it returns, goes to the next line. The next line goes up to here, passes in an again. So self also points to this object, that now has a 1 in it, and it comes in. Self.x = self.x + 1, so that’s a, it pulls this variable out. Adds 1, this becomes 2, it goes back into self.x, so that becomes 2. Then we print this out, So far 2, then we finish. Go down to the next line, comes back up, runs it again. This becomes 3, and then it prints out, so far self.x is 3. And so that’s the logic of it. It’s just kind of a, so far, it’s like a clever way to hide or embed a function inside this template. Now, in reality they would have multiple methods and multiple bits of data. And so it’s more complex than this. But, for now, this sort of just gets us started to understand the syntax of the class. What we call the moment of construction. This is the moment of construction right here, where it’s being constructed. And these are the invoking of the methods within the object. Now, like we’ve shown, the type() and the dir() tell us what kind of things these are. And the dir() tells us what the methods available are. If we take a look at the thing we just created, so class PartyAnimal. And we make a new PartyAnimal and assign it into an. And we say, hey, what is the type of an, and what is the methods that are available in an? It will basically say, an is of type class ‘ main .PartyAnimal’. That’s what it is, and so it says that’s the kind of thing that this is. The template from which we created an is this template right here. And so it knows that, and it remembers that. And then dir() says, what are the things in there? And you see a bunch of things with underscores. Those are internal, but eventually, you see party, which is this method. And you see x, which is this attribute. And so dir() is looking or inspecting inside here, and saying, well, what things are in here? And you see party and you see x. And so dir(), when dir() is looking into a string, it’s doing the same kind of thing. It’s like, oh, what little functions live inside of strings? And that’s what we do, we have like strip, split, strip, upper, right? Those are the functions that are like in the code that someone wrote a long time ago to make strings and define strings. And again, it works the same for classes that we define, and then create an instance of, and then use. So up next, we’re going to look into a little more detail of this notion of constructing and destructing multiple instances of objects.

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

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

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