Passing source code to __init__ to override method dynamically(Table driven subclassing)
Pettersen, Bjorn S
BjornPettersen at fairisaac.com
Fri Nov 21 02:10:10 CET 2003
> From: Ian Sparks [mailto:Ian.Sparks at etrials.com]
> I want to create specialized instances of a base class
> without declaring descendant classes.
> Instead, I want to do the specialization by passing
> source-code to the constructor of the base-class. This way I
> can make my program table-driven.
At this point stop and ask yourself why:
- being table driven is a good thing?
- having a common base class is useful?
> Here's my working (but sucky) code :
# I belive this is equivalent (untested).
code = """ ... """
x = new.function(compile(code,"<string>",'exec'), globals())
> Any advice?
You're being too clever... or not clever enough <wink>. I have a feeling
you might be looking for the new import machinery...
Designing your classes AS CLASSES rather than as bunches of
vaguely related snippets of source would undoubtedly be a
far better way to proceed.
-- Alex Martelli
More information about the Python-list