N
Neil Cerutti
This code is from The Python Cookbook, 2nd edition, 12.2 Counting
Tags in a Document:
from xml.sax.handler import ContentHandler
import xml.sax
class countHandler(ContentHandler):
def __init__(self):
self.tags={}
def startElement(self, name, attr):
self.tags[name] = 1 + self.tags.get(name, 0)
Isn't overriding __init__ a risky thing to do? The docs don't
mention it as a method I should override, and also don't define
what's in there or if I'd need to call the base class __init__.
Moreover, startDocument is provided for parser setup.
As it happens, ContentHandler.__init__ isn't empty, so the above
code could fail if the parser isn't prepared for _locator to be
undefined.
Is the above code is an acceptable idiom?
Tags in a Document:
from xml.sax.handler import ContentHandler
import xml.sax
class countHandler(ContentHandler):
def __init__(self):
self.tags={}
def startElement(self, name, attr):
self.tags[name] = 1 + self.tags.get(name, 0)
Isn't overriding __init__ a risky thing to do? The docs don't
mention it as a method I should override, and also don't define
what's in there or if I'd need to call the base class __init__.
Moreover, startDocument is provided for parser setup.
As it happens, ContentHandler.__init__ isn't empty, so the above
code could fail if the parser isn't prepared for _locator to be
undefined.
Is the above code is an acceptable idiom?