import psyco
class Rectangle(psyco.compact):
def __init__(self, w, h):
self.w = w
self.h = h
def getarea(self):
return self.w * self.h
r = Rectangle(6, 7)
assert r.__dict__ == {'w': 6, 'h': 7}
print r.getarea()
The above example runs without using Psyco's compilation features. The r instance is stored compactly in memory. (Thus this feature of Psyco probably works on any processor, but this hasn't been tested.)
The same example using the metaclass:
import psyco
class Rectangle:
__metaclass__ = psyco.compacttype
def __init__(self, w, h):
self.w = w
self.h = h
def getarea(self):
return self.w * self.h
r = Rectangle(6, 7)
assert r.__dict__ == {'w': 6, 'h': 7}
print r.getarea()