Passing source code to __init__ to override method dynamically(Table driven subclassing)
Ian Sparks
Ian.Sparks at etrials.com
Fri Nov 21 09:06:18 EST 2003
>>From: Pettersen, Bjorn S [mailto:BjornPettersen at fairisaac.com]>
>>At this point stop and ask yourself why:
>>
>> - being table driven is a good thing?
>> - having a common base class is useful?
Thanks Bjorn,
The rational is that the application I have in mind allows (experienced, trusted) end-users to store "decision making" code in a database table. This can be as simple as "if self.somevalue < self.someothervalue then PASS else FAIL." There really is no need for those end-users to have to add all the template code to make a valid subclass.
I want to override a method in order to get access to the "self" namespace which provides access to everything in the environment needed to make these "decisions" and to process a PASS or FAIL.
Perhaps my efforts *are* misguided in which case I'd appreciate any advice you can give.
Finally, thanks for the tip but I'm having trouble taking a function created :
import new
code = """ ... """
x = new.function(compile(code,"<string>",'exec'), globals())
and adding it as a method to my existing class as a replacement to my existing function. I'll keep experimenting.
- Ian
-----Original Message-----
From: Pettersen, Bjorn S [mailto:BjornPettersen at fairisaac.com]
Sent: Thursday, November 20, 2003 8:10 PM
To: Ian Sparks; python-list at python.org
Subject: RE: Passing source code to __init__ to override method
dynamically(Table driven subclassing)
> 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).
import new
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...
-- bjorn
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
mailing list