lazy (?) evaluation, request for advice
Mike Fletcher
mfletch at tpresence.com
Wed Jan 5 18:44:12 EST 2000
Idea:
class Hand:
def losers( self ):
def spades( self ):
# setup global environment
env = {"whatever":somefunction, "whenever":somevalue }
selected = []
for hand in somelistofhands:
# setup specific environment
for boundfunction in [ "losers", "spades", "hearts"]:
env[ boundfunction ] = getattr( hand, boundfunction )
if (eval( userfunction, env )):
selected.append( hand )
env.clear() # to eliminate references to the last hand's methods
So the user then writes the following:
if (spades() > 6) or (3.0 < losers() < 4.0):
whatever()
That is, define an environment in which the code runs, then put simple
functions in that environment to take care of the lazy evaluation explicitly
for the given algo. Alternatively, you could poke around in the internals
of a function definition picking up names and deciding which elements to
calculate, but then you're still calculating losers for every hand in the
above example, instead of only doing it when spades() > 6 .
HTH,
Mike
-----Original Message-----
From: Alex Martelli [mailto:alex at magenta.com]
Sent: Wednesday, January 05, 2000 6:17 PM
To: python-list at python.org
Subject: lazy (?) evaluation, request for advice
...
(spades>=6 or hearts>=6) and (3.0<losers<4.0)
rather than have to mention "current_hand.spades" or
other such explicit notation.
...
More information about the Python-list
mailing list