new.instancemethod
Alex Martelli
aleaxit at yahoo.com
Thu May 17 18:09:28 EDT 2001
<James_Althoff at i2.com> wrote in message
news:mailman.990130053.9452.python-list at python.org...
...
> IOW, for most of the examples I see where "instance-specific behavior" is
> useful a reasonable strategy is for the instance to delegate immediately
> back to the object/context that created (or uses) it to do something
> meaningful on its behalf.
>
> Given that, I was wondering if anyone had a good example from actual
> practice where the "instance-override-of-method"-type approach was the
best
Oh, *THAT*. Sure. For example, I have a lot of objects I'm analyzing,
each represents a bridge hand and is an instance of a big class. Now I
get a subset of the objects by some criteria and pass them to a new
module I'm developing and just loaded dynamically into the framework.
The module selects those objects to which a certain behavior I'm studying
would be applicable and now needs to change the behavior of those objects
for a few situations, then get back to the framework for a simulation run.
In the framework I don't know exactly what out of the dozens or hundreds
of behavior-snippets represented by methods in a bridge hand the new
module may want to tweak and change with its own functions.
In C++ I'd have to overdesign a Strategy design pattern representing
ALL possible behaviors of a bridge hand, I guess. *shudder* hardly what
one would call a reasonable approach to exploratory programming!-).
In Python it's trivial. Say the behavior I'm studying is: "for a hand which
by basic methods would bid a natural invitational 2NT, bid 3NT instead
if it has a 6-carder or longer suit header by the A". The module will
easily find "all hands with a 6-carder or longer headed by A" and for
them, and them only, change the .ownbid method:
def ownbid_tweaked(self, bidding_history):
bid = self._save_ownbid(bidding_history)
if str(bid)=='2N' and bid.force()=='INVITE':
return bid(3, 'N', 'SIGNOFF')
return bid
def tweak_ownbid(obj):
obj._save_ownbid = obj.ownbid
obj.ownbid = new.instancemethod(ownbid_tweaked,
obj, obj.__class__)
(Sorry, just improvising -- don't have the original example at hand,
so this is untested, but I hope it gives the general idea!).
I'm sure contract-bridge research is not the only exploratory simulation
based research field in which this would apply -- changing behavior of
a few objects in certain cases, without bothering to make new classes.
A class-centered person would no doubt prefer a class-focused approach
here too:
def tweak_ownbid(obj):
Base = obj.__class__
class Tweak(Base):
Base = Base
def ownbid(self, bidding_history):
bid = Base.ownbid(self, bidding_history)
# &c, snipped
obj.__class__ = Tweak
this has its advantages, but keeping these per-method decorators
as instance-specific/method-specific entries has pluses too, most
particularly if you also start removing and juggling them around
more freely.
Alex
More information about the Python-list
mailing list