字幕列表 影片播放 列印英文字幕 - Okay so now that we've gotten through the definitions, let's work into some sample code. But hey, look at this, we've got ourselves our cookie cutter and some cookies. So remember that a class is a template, it's not the actual thing. An object is an instance of a class. So you have to take the class and do something to make the object. And actually you can see here some other classes, there's clearly sort of a snowflake class and a gingerbread man class. That's an object, object, object. Somewhere out here there is a snowflake class and a gingerbread class, but we got a snowman object and a snowman object and a snowman class. So class is the template, object is the instance. So here's a better python code. So let's take a look at what we got here. Classes is in the reserved word, kind of like def. We have the name the class, that is a name that we choose. That's the name by which we'll refer to this class for the rest of this program. And it has a colon at the end of it, in which it starts an indented block. Which ends when we de-indent. Inside the class, there are generally two things. There is some data and this just looks like an assignment statement in the class. X equals zero. And then there is a def. This looks just like a function, in that it starts with a def, has a colon, indents. So that function finishes right there. The difference is that this is a method, because it lives inside of a class. And so there is no function called party. There's a function called party within party animal class. And we'll talk in a second about this self thing. It is the way that inside this code we refer back to that variable. So this is not actually executing any code. It's sort of remembering the template, defining the class party animal. This is what we call constructing. Using the party animal template or class, we are making a party animal. And then once we make that, we stick it in the variable an. And then we're going to call this party method three times, one, two, three. Now this self thing, and we'll take a look at the self. The self ends up being an alias of an. And so you can look at this syntax, it's just kind of an equivalent of this syntax. It's calling the party method within the party animal class, and passing the instance in as the first parameter. And so self ends up being an alias of an each time these are called. Now if we make a different variable and a second object, which we will eventually, you will see that that works a little bit differently. And so, this syntax is a short version of that syntax. So if we watch how this executes, it's oops. It starts up here. It just defines it. And then we construct it. And that's what basically constructing it, we know how to construct it because we look at the class, and we make a variable x, we make some code party. And then we construct that, that's what the party animal does. And then we assign that into an. And so, an is now pointing at that. Then when we call the party method, that basically takes this an and passes it in as the first parameter, which is used as self. And so, self.x, which is what we're doing in this line right here. Self.x is a variable. X starts out as zero. X starts out as zero because when it was constructed it was set to zero. So we're in here, an is an alias of self and now it looks up self.x, which is zero, adds one to it so this becomes one. And then we print so far, so far one. And then the code returns and it goes down and does it again. And x becomes two. Prints out so far two, comes back down and does the last time, calls it again. Self.x is two, add one to it and stick it back in. So this becomes three and we print out three. And then the program finishes. And so you can think of this as constructing the object and then associating it with this an variable. Now we've created this object, we can play around with things we've playing around before with dir and type. We use dir and type to inspect variables and types and objects. So we've been using objects all along. This code here says, hey make me an empty list. Well it turns out that what we're saying is that there's already a list class inside a python and we're constructing an empty list and when we get back this empty list, we're assigning that into x. So x in a sense, contains or points to an empty list. So then we say, hey what is in x? What kind of thing is x? Well it's a list. This is a thing, it's a list type. Lists have list of things in them. And use a pen and all the things we've been doing before, they're just objects. And then the dir, if you remember the dir, the dir is the capabilities. And there's all these internal capabilities that do things like implement the bracket operator et cetera. There's double underscore ones. We can ignore them. Although you can even look them up and figure out what they mean, if you feel like it. But the methods that we tend to call are in this class. And so things like x.sort. I've always told you, that is the sort method within the x thing. And the dot operator is the operator that we use to look something up within an object. So you've been using this syntax all along. X.sort, dictionary.items, all of those are methods within the corresponding class. If we take a look at this line of code that we've been doing for a very long time, which says, oh stick hello there into y. If I reword that, is more OO or object orientated, what this single quote does, says make me a string object, and put some text in it and then when that is done being constructed, stick that into y. And so y now points to a string object that's been pre initialized to the string, hello there. Now that's a long way of saying hello there ends up in y. But in OO terms, we can talk about that. If we do a dir of that, we see a whole bunch of internal methods, which have double underscores and then we see all kinds of methods that we've been using. We've been using methods like upper. We've been using methods like find. We've been using methods like rstrip, right? We've been using these methods. So we gonna like y.rstrip(). Again that's a method, that's an object, not a class, it's an object. And that is the object look up operator. Now if we do the same thing to code that we've built or a class that we've built. So now we have a party animal class. Remember this up to here is just definition. Now we construct it and we store it in an. So an is a variable that contains an object of type party animal. We asked it what type it is and it prints out here. It says, this is a class. And it's main_partyanimal in this whole thing here is the _main, it's scoped _main. But you can see that you have made a new type. You built a type by using this class keyword. And then we use the dir, remember dir looks for capabilities. And again, you'll see a whole bunch of underscore things. They have meaning, you can look them up. But eventually you'll see the two things that you've put in it. One is the method party and the other is the attribute or field x. And again, these are the things that you can say an.x or an.party. Because this dot is the object operator, the object lookup operator that says look up in the object an the thing x. Or look up in the object an the thing party. Okay? So up next we'll talk a little bit about how objects are created and destroyed. We also call that, object life cycle.
A2 初級 美國腔 PY4E - Python 對象 (第 14 章 第 2 部分) (PY4E - Python Objects (Chapter 14 Part 2)) 10 0 劉馨兒 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字