Announcing bytecodehacks 0.10

Michael Hudson mwh21 at cam.ac.uk
Wed May 12 15:28:42 EDT 1999


If you've been following the "Python is too slow" thread of late, you
might have noticed my sneaky bytecode rewriting functions.

Well, they've been rewritten, added to and generally banged on, and
remarkably enough, there's nearly full documentation (Thanks to Fred
Drake and all the others for making making documentation so easy).

The package (and that's how it's distributed) splits into two parts -
the byte code editing routines and the "bytecodehacks" that are
usuable without a degree in python arcanery, although one might help
understand some of the consequences.

Some hilights:

bytecodehacks.closure - bind global references to constants

An example:

from bytecodehacks.closure import bind

def make_adder(n):
    def adder(x):
        return n+x
    return bind(adder,n=n)
add1=make_adder(1)
add(1) => 2

bytecodehacks.xapply - a sort-of lazy apply

from bytecodehacks.xapply import xapply

def add(x,y):
    return x+y
add1=xapply(add,1)
add1(1) => 2

bytecodehacks.setq - this one should interest people!

You want assignment expressions? Have them!

def f(x):
    while setq(x,x-1):
        print x
from bytecodehacks import setq
g=setq.setqize(f)   
g(4)
3
2
1

bytecodehacks.inline - Python gets inline functions

>>> from bytecodehacks.inline import inline
>>> def ff(x):
...  f(x)
...
>>> def g(x):
...  print x
...
>>> def h(x):
...  print "h", x
...
>>> inline(ff,f=g)(2)
load found at index 2
found call
2
>>> inline(ff,f=h)(2)
load found at index 2
found call
h 2
>>> 

Please note that these modules are not really bullet-proof, more a
proof-of-concept than anything else.

The are also public domain; do what you like with them. If you find
bugs, or more imaginative uses for these techniques, I'd surely like
to know!

Get the bytecodehacks at

http://starship.python.net/crew/mwh/

The docs are at:

http://starship.python.net/crew/mwh/bytedoc/bch/index.html

Enjoy.

Michael Hudson
Jesus College
Cambridge
CB5 8BL
mwh21 at cam.ac.uk




More information about the Python-list mailing list