Python Desktop Server Weblog 25.12.2004

a picture of myself

Münsterland.org

2004-12-25

Python 2.2 and extended slicing

Can it be that although extended slicing is supported in Python 2.2, sequences (lists, tuples, strings) only support simple slicings? This is rather weird - __getitem__ get's passed in a slice object in Python 2.2, but the builtin types don't accept those slice objects as index values - and they don't accept the extended slice syntax at all.

l = range(0,10)
print l[1:5]     # this works
print l[1:5:2]   # this barfs

Weird. Really weird. Makes life not easier with my LazyEvaluation stuff - I want to support Python 2.2 and so need to hack something up that will work with it. Looks like I will still have to overload __getslice__ and friends ...

This post references topics: python
posted at 12:03:28    #
 

Making the impossible possible

Lazy evaluation is a way to delay execution to some later point in time - you build up a promise to deliver the result, but you only deliver it when it really is needed. As long as it isn't needed, it isn't calculated. Lazy evaluation is usefull when you have complicated calculations whose result might not be needed at all - but you don't know up front.

Usually you do it by boxing the expression in some closure and calling that closure later on when you need the value. Easy as pie. But this has a problem: what if you have a data structure built by dictionaries, objects with methods, attribute access and stuff like that? Of course you can box the full expression - but that would require that you know the whole expression up front. What if the expression is built piece by piece over time?

Lazy languages push this concept over the border: all expressions are evaluated lazily. But this requires a new language - normal languages with strict evaluation don't allow grafting lazy evaluation onto them after the fact.

So I thought what is needed is something like faked lazy evaluation in Python. Yeah, that's it - it quacks like lazy evaluation, it smells like lazy evaluation and it mostly looks like lazy evaluation. So let's call it lazy evaluation Winky

Using it is easy:

from LazyEvaluation import *

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

f = lazy(anton)

print repr(f(5,6))
print f(5,6)

The first print will print a Promise() instance representation. The second print will print 11 - because print does str() on it's arguments and that will call the magic method __str__ and that one will first force the evaluation of the Promise() and then apply the original method.

I know I am crazy. And maybe all this is just stupid. But it was fun to hack. It makes heavy use of magic methods, meta classes, closures. Hey, if I already had Python 2.4 installed I would even have used decorators (it should fit into 2.4 quite fine - just use lazy as a decorator if you want to make one of your methods lazy evaluated). You have been warned. Winky

This post references topics: python
posted at 03:10:08    #
December 2004
MoTuWeThFrSaSu
   1 2 3 4 5
6 7 8 9101112
13141516171819
20212223242526
2728293031  
Nov
2004
 Jan
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

© 2004-2007, Georg Bauer