1. Technology

Part 3: Creating a Feed Class and Finishing the Python Program


7 of 8

A Class Without An Object is Useless

Having finished the class ModelFeed, we need to write a function that initiates an object of this class. This will enable us to retrieve data by feed name. As the links will form the body of the HTML document, let's call this function formBody. The code looks like this:

 def formBody(feedname): 
      feed = ModelFeed() 
      feedurl = feed.feeddata(feedname) 
      body = feed.getlinks(feedurl) 
      return body 

If you have read my tutorials on functions and classes, this is straightforward. After declaring that 'formBody' requires the variable feedname, we initiate an object feed of class ModelFeed.

We then pass feedname as an argument to the method feeddata of the new object. This gives us feedurl, the address of the feed.

The address is then passed to the method getlinks. This method returns the headlines and links in HTML format and may thus be assigned to body and returned.

You may ask why we do not simply put all of this into main(). After all, we are only using it one time. The more that one can abstract a problem in software development, the more modular the design is. Modular design makes it easier to troubleshoot the program, to maintain the program, and to extend the program. If you ever want to do something else with the returned data from getlinks, you do need to gut main() and write the function. What's more, because it is Python, you can call all the classes and functions defined here from other Python programs. If you are unsure how to do that, see my tutorials on importing modules.

  1. About.com
  2. Technology
  3. Python
  4. Web Development
  5. RSS Reader in Python - Building an RSS Reader With Python - A Class Without An Object is Useless

©2014 About.com. All rights reserved.