课程: Python Essential Training

Classes and objects - Python教程

课程: Python Essential Training

Classes and objects

(upbeat music) - We already talked about a toaster function, but what if we want to make a whole kitchen? A kitchen has many abilities, toast, bake, microwave, wash dishes, and of course, the all-important coffee maker. These various kitchen appliances have settings, the knob on the toaster, the power setting of the microwave. The dishwasher itself has different modes and multiple functions that can apply to dishes. I'm sure we could make a whole bunch of functions and variables, and throw them in a file somewhere. But what if we want two separate kitchens with different abilities and sets of appliances? What if we want to make a whole house with a kitchen, a laundry room, a bathroom? Multiple houses, maybe one with no laundry room that has to outsource its laundry to a laundromat? You can imagine how these systems would lead to dozens or hundreds of functions, thousands of variables, code that gets long and unmanageable pretty quickly. Fortunately, programmers have invented a tool to deal with situations like this, the class. A Python class is a great way to keep related collections of functions and attributes labeled and organized. Let's go to the code. The classic way to demonstrate classes in programming is by using animals, so let's make a class called Dog. So we need to define a class with a name and you get to use an uppercase letter for class names. This is what makes them different from variable and function names. Classes usually start with uppercase letters. And because we've typed a colon, this indicates that we're going to indent to the next line. And everything inside this indent is going to be our class definition. Now we can start defining all of the functions and attributes that a dog has. And the first function that we want to define is a special one, the init function. Init stands for initialization, and this function is called whenever an instance of the class Dog is created. It has two underscores on either side, and this is a special Python defined thing so the computer will recognize this as the initialization function. Then we're going to pass in a variable called self. This variable is the specific instance of the Dog class after we've initialized it. And, well just watch, this is going to make more sense in a minute. We're going to define a couple attributes and functions in here. We want to make a dog with four legs and a name, and we'll call it Rover. So name is equal to Rover and self.legs equals four. We're also going to make a function called speak that prints out bark, print bark. Notice that I passed the self variable to the speak function. This means I have access to any of the attributes or functions in this class. For instance, I could do this, self.name plus, says bark. And this will concatenate the dog's name with this string here. Now let's use this class. Let's just run that. And then my dog is equal to dog. So my dog is equal to an instance, a newly created instance of the class Dog. Now this is in text with the parentheses makes it look a little like a function but Dog is a class, not a function. And this is why it's important to adhere to the Python convention of keeping variables and function names lowercase and only class names are capitalized so that you can tell the difference between the two. When we do this, the initialization function gets called and we get an instance of our Dog class returned. We can create another dog. It's equal to dog. Okay, so now we have my dog and another dog. So let's do my_dog.speak. Rover says bark. And another_dog.speak. Ah. It's always possible to have two dogs named Rover, it's a very common name, but this starts to get a little bit confusing. What if we want to give dogs different names and we want to maybe provide a name when we initialize the dog? All we have to do is this, pass in a name variable and say self.name is equal to name. Then when we create each dog, we pass the name in, Rover and Fluffy. Rover says bark and Fluffy says bark. Much better. Notice that the speak function, when you call it, it looks like it doesn't have any arguments. There's nothing being passed in these parentheses. But if we look at the function definition in the class it does have one argument, self. And the way Python works is that if you have a class instance, like these two things are instances of the class Dog, and you use the dot function syntax, it acts like your instance was actually passed in as the first variable of this function. So this self variable here is literally the instance of the class that the function was called on. So if you've heard of the term object oriented programming before, this is what it is. These class instances are also called objects. These are dog objects. The variables inside these classes are called attributes, attributes of the objects. The functions in here are called methods and you can call them class methods. We say that the Dog object has the method speak. So this is a very brief introduction to some of the concepts and terminology of object oriented programming, but I'm going to be showing off really how powerful they can be later on.

内容