Overriding variables used by base classes

Corey Lubin google_mail at aristoweb.net
Fri Nov 14 16:06:36 EST 2003


It seems that in trying to present simple concept code, for the
purpose of example, I forgot to mention some of the restrictions on
what can be done. original.py is not of my own creation; it's a
seperately maintained module, external to the project, and thus cannot
be modified.

For those that care to hear the full background/details of this
problem: I make use of HTMLParser ("Original", from the example), but
it's not resilient enough for real world pseudo-HTML, so I decided to
modify it through extension. I've subclassed it (currently with no
overriden methods, in an attempt to allow the subclass to benefit from
changes to the base class) and attempted to override a global variable
that the base class uses, however my approach doesn't work. In this
case, "doesn't work" means that, in terms of my example, Tainted.foo
uses original.someGlobal rather than tainted.someGlobal. This happens
presumably because Tainted.foo is actually Original.foo (the foo
method is inherited) and inherited methods apparently don't pay any
mind to the namespace of their inheritors (take note that I'm not
saying that they /should/; I haven't given any thought to that; I'm
just saying that I expected this approach to work). This apparent
failure in my approach leads me to think that eval/exec is my
solution; I'm just hoping to hear that there is something more simple
than that.

My "import everything into this namespace and just modify what I need
to" approach was my attempt at duplicating the original module (for
miniature modification) without actually duplicating it's code (so
that my modified version won't grow stale as the original module's
code is updated by it's maintainer).

Example translation guide (for those that feel the details make a
diff.):
original.py = HTMLParser.py
original.someGlobal = HTMLParser.locatestarttagend (a regex object)
original.Original.foo = HTMLParse.HTMLParser.check_for_whole_start_tag

I hope this clears some things up.

Corey




More information about the Python-list mailing list