Let's refine our word fetching module a little further. First, we'll perform a small refactoring and separate the word retrieval and collection on the one hand from the word printing on the other:
from urllib.request import urlopen # This fetches the words and returns them as a list. def fetch_words(): with urlopen('http://sixty-north.com/c/t.txt') as story: story_words = [] for line in story: line_words = line.decode('utf-8').split() for word in line_words: story_words.append(word) return story_words # This prints a list of words def print_words(story_words): for word in story_words: print(word) if __name__ == '__main__': words = fetch_words() print_words(words)
We do this because it separates two important concerns: when importing we'd rather get the words as a list, but when running directly, we'd prefer the words to be printed.
Next, we'll extract...