Static and instance methods
- [Instructor] Let's take a look at one my favorite things to do in Python, String Parsing. I'm going to make a class called Word Set, and like its name implies, it is going to contain a set of words. So self dot words is equal to an empty set. So this set starts out empty, and as we go, we can add to it. To add to it, I just want to pass in big blocks of text. Like many programmers, I'm fairly lazy, and I don't want to have to clean my text, or do anything special before I pass it in, so I'm just going to pass it in, punctuation, and all. Word set, it's going to be equal to new word set. Word set, dot, add text. Hi, I'm, let's escape that, escape that apostrophe there. Here is a sentence I want to add, then word set dot add text. Let's make another sentence. Here is another sentence I want to add, period. Okay, so obviously we need to write a method here called add text, and that's going to take in self and some text. The first thing, add text is going to do is called clean text, self dot, clean text, text. Another method we want to write def, clean text, self text. All right, so clean text is going to take in some text, then remove the punctuation. Text is equal to text dot replace. Replace the exclamation point with an empty string. Replace the period with an empty string, Replace that with an empty string. Replace, we need to replace that apostrophe with an empty string as well. Okay, so notice that I can call these replace functions one right after the other like this. And if you remember from previously in the course, this is called chaining functions. You don't want to go too overboard with this, but it helps group all these punctuation replacements together and just makes it look nice on one line. Then we're going to return, text dot lower. Make everything lowercase after stripping out the punctuation, then let's fill in the rest of add text, so it cleans the text. Then for word in text dot split. Self dot words dot add word. Remember the split function splits a string on a space. It splits the string on each space, and transforms that into a list. So this will take a sentence, and turn it into a list of words. And finally, let's print word set dot words. All right, so that looks pretty good. One minor thing you might have have noticed though, clean text has this self instance passed in, but it never actually gets used. Self is a completely unused variable. If we try to remove it 'cause we don't need it, right? And we run this class, we get an error because we're still passing the self instance into the method, but it only takes a single parameter now. So let's remove self up here, run it again. Well, now we get another error. So this now has no idea where to look for the function clean text. So it says that it's not defined. It's expecting it to be defined somewhere out here, but instead, it's defined in the word set class. So we can actually fix this by doing that, and that works, too. Now this method, clean text is what's called a static method. By removing the self, the class instance from the parameters that get passed into this method, we've said this method doesn't belong to any particular class instance. It's actually part of the word set class definition itself. And programmers call these static methods in the sense that they're unchanging, they're not dynamic, they're static. So traditionally, static methods are used to hold constants, unchanging variables, fundamental business logic, and that sort of thing. As a side note, methods like add text are instance methods. They're methods that belong to a particular instance of the class. So you have static methods and instance methods. In addition to static methods, there are also static attributes. We saw an example of this previously with the legs attribute on the dog's class. That's a static attribute because it's part of the class definition rather than being associated with a particular class instance. So let's make a static variable called replace puncs, and we can add this to our word set class to control which punctuations get replaced. So we want to replace the exclamation point, the period, the comma, and that quote there. Okay. And we can use it like this. Let's just copy this, put it down there. Let's make a new class. And then let's say, for punc, word set dot replace puncs, text is equal to text, dot replace, punc empty string. All right? And we get the same results. So with static variables, we have the option to use the the class name word set or the class instance self. If of course, we were passing self in here, but we can use either self or the class name interchangeably. But remember, we can't do this with self dot clean text, right? We can't do that because that would actually pass the instance into this function. So there's a little bit of a dilemma there. We have to use word set. However, we can solve this with a nifty trick in Python and that is called a Decorator. So let me copy this, bring it down here, and I'm going to add static method. So this is called a decorator and a decorator in python is an annotation, or description for your function definition. It actually belongs to the function definition there. So decorators always start with the at sign. So this is the static method decorator. And your decorators just defined some special attributes or information about the function, so that Python knows how to handle it. And now that we've explicitly told Python that clean text is a static method, that self should not be passed in as an argument, we can actually do this. And if we run it, there you go. Keep in mind you don't have to use the static method decorator. I usually don't unless there's a good reason to. Depending on the company you work for, they may require you to use it just as a matter of style. There are many, many ways to write the same piece of code. I encourage you to develop your own style as you progress with Python.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。