Python Desktop Server Weblog 17.8.2005

a picture of myself

Münsterland.org

2005-08-17

Simpler Lazy Evaluation with Python

If you know before what kind of value your function call will return, you can build a very simple decorator for functions or methods that will turn them into lazy evaluation ones. It goes like this:

class NoneSoFar: pass
NoneSoFar = NoneSoFar()

def lazy(func, resultclass):

 
class __proxy__:

   
def __init__(self, args, kw):
     
self.__func = func
     
self.__args = args
     
self.__kw = kw
     
self.__result = NoneSoFar
     
for (k, v) in resultclass.__dict__.items():
       
setattr(self, k, self.__promise__(v))

   
def __promise__(self, func):

     
def __wrapper__(*args, **kw):
       
if self.__result is NoneSoFar:
         
self.__result = self.__func(*self.__args, **self.__kw)
       
return func(self.__result, *args, **kw)

     
return __wrapper__

 
def __wrapper__(*args, **kw):
   
return __proxy__(args, kw)

 
return __wrapper__

To use it is very simple and straight forward:

def anton(a,b):
    
return a+b

anton = lazy(anton, int)

print type(anton(5,6))
print str(anton(5,6))

This should print the __proxy__ instance in the first line and the real value in the second one. The proxy class automatically defines special methods for every method that's defined on the result type. This works best for builtin types, for more complex types you would have to take precautions not to stomp over important attributes.

And as you can see this won't help with ambigiously defined functions - anton could work with strings, too, but the lazy stuff will only install handlers for int magic methods.

But as the title writes: it's about simpler lazy evaluation, not the full thing Winky

This post references topics: python
posted at 19:18:56    #
August 2005
MoTuWeThFrSaSu
1 2 3 4 5 6 7
8 91011121314
15161718192021
22232425262728
293031    
Apr
2005
 Dec
2005

This is the Python Desktop Server weblog.


(Donations will be used by the author to buy stuff, fullfill selfish wishes or do other silly recreational things. You have been warned.).
The PyDS is
OSI Certified Open Source Software

Python Powered

XML-Image

© 2005-2007, Georg Bauer