Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53305 > unrolled thread
| Started by | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| First post | 2013-08-30 17:20 +0000 |
| Last post | 2013-08-30 17:35 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
sax.handler.Contenthandler.__init__ Neil Cerutti <neilc@norwich.edu> - 2013-08-30 17:20 +0000
RE: sax.handler.Contenthandler.__init__ "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> - 2013-08-30 17:35 +0000
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-08-30 17:20 +0000 |
| Subject | sax.handler.Contenthandler.__init__ |
| Message-ID | <b8c2igFiupsU1@mid.individual.net> |
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?
--
Neil Cerutti
[toc] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> |
|---|---|
| Date | 2013-08-30 17:35 +0000 |
| Message-ID | <mailman.395.1377884156.19984.python-list@python.org> |
| In reply to | #53305 |
Neil Cerutti wrote:
> 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?
>
> --
> Neil Cerutti
> --
I think this is a bad idea unless you want to avoid the parent
class __init__ specifically (in which case a comment stating
why is mandatory). I do not like that this recipe shows behavior
that might be fine in this instance, but is not a good general
practice.
def __init__(self):
super(ContentHandler, self).__init__()
#OR ContentHandler.__init__(self)
self.tags={}
I personally think the super() line is better of the two options.
~Ramit
This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web