From the course: Python Essential Training
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Static and instance methods - Python Tutorial
From the course: Python Essential Training
Static and instance methods
Let's take a look at one of my favorite things to do in Python, string parsing. I'm going to make a class called WordSet. And like its name implies, it is going to contain a set of words. So self.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 is 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 wordset.addText, 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 addText, and that's going to take in self and some text. The first thing addText is going to do is call cleanText, self.cleanText text, another method we…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.