Dumb overloading question

Will Ware wware at world.std.com
Wed Jun 21 18:05:56 EDT 2000


I'm feeling a little idiotic and hope somebody can point out the dumb
thing I'm doing here. I'm trying to write two SGML parsers that translate
my source document to an HTML page, and a set of slides in Postscript.
I wrote the HTML generator first, so I have:

---- begin s2html.py ----
import sys, sgmllib
class HTMLParser(sgmllib.SGMLParser):
    def handle_data(self, data):
        self.mydata = self.mydata + data
    # then define many functions that make calls to
    # self.handle_data, for example...
    def start_source(self, attrs):
        self.handle_data('<html>')
    def end_source(self):
        self.handle_data('</body></html>')
# other extraneous stuff elided for brevity
---- end s2html.py ----

Then I decided that the Postscript generator should start off using
most of the same behavior.

---- begin s2ps.py ----
import sys, s2html
class SlideParser(s2html.HTMLParser):
    def __init__(self):
        s2html.HTMLParser.__init__(self)
        self.inSlide = 0
    def handle_data(self, data):
        if self.inSlide:
            self.mydata = self.mydata + data
    def start_slide(self, attrs):
        self.inSlide = 1
        s2html.MySGMLParser.start_slide(self, attrs)
    def end_slide(self):
        s2html.MySGMLParser.end_slide(self)
        self.inSlide = 0
---- end s2ps.py ----

The wierd thing is that when I use SlideParser, I get behavior as if
HTMLParser's functions (such as start_source) are using the HTMLParser
version of handle_data instead of the SlideParser version. Isn't that
incorrect? When I overload handle_data, shouldn't that apply to the
functions in HTMLParser who make calls to handle_data? I am befuddled
and would appreciate any clarification.
-- 
 - - - - - - - - - - - - - - - - - - - - - - - -
Resistance is futile. Capacitance is efficacious.
Will Ware	email:    wware @ world.std.com



More information about the Python-list mailing list