from math import sqrt
-def complex(re, im):
- return Complex().init(re, im)
+class complex:
-
-class Complex:
-
- def init(self, re, im):
+ def __init__(self, re, im):
self.re = float(re)
self.im = float(im)
- return self
+
+ def __coerce__(self, other):
+ if type(other) == type(self):
+ if other.__class__ == self.__class__:
+ return self, other
+ else:
+ raise TypeError, 'cannot coerce to complex'
+ else:
+ # The cast to float() may raise an exception!
+ return self, complex(float(other), 0.0)
def __repr__(self):
return 'complex' + `self.re, self.im`
def test():
a = complex(2, 0)
b = complex(3, 4)
- print a, b
- print a+b, a-b, a*b, a/b
- print b+a, b-a, b*a, b/a
+ print a
+ print b
+ print a+b
+ print a-b
+ print a*b
+ print a/b
+ print b+a
+ print b-a
+ print b*a
+ print b/a
i = complex(0, 1)
print i, i*i, i*i*i, i*i*i*i
j = complex(1, 1)
start, stop, step = a
else:
raise TypeError, 'range() needs 1-3 arguments'
- return Range().init(start, stop, step)
+ return Range(start, stop, step)
# Class implementing a range object.
class Range:
# initialization -- should be called only by range() above
- def init(self, start, stop, step):
+ def __init__(self, start, stop, step):
if step == 0:
raise ValueError, 'range() called with zero step'
self.start = start
self.stop = stop
self.step = step
self.len = max(0, int((self.stop - self.start) / self.step))
- return self
# implement `x` and is also used by print x
def __repr__(self):
class BitVec:
- def init(self, *params):
+ def __init__(self, *params):
self._data = 0L
self._len = 0
if not len(params):
else:
raise error, 'bitvec() requires 0 -- 2 parameter(s)'
- return self
-
-
- def _init(self, data, len):
- self._data = data
- self._len = len
- return self
-
def append(self, item):
#_check_value(item)
#self[self._len:self._len] = [item]
self[self._len:self._len] = \
- BitVec()._init(long(not not item), 1)
+ BitVec(long(not not item), 1)
def count(self, value):
def insert(self, index, item):
#_check_value(item)
#self[index:index] = [item]
- self[index:index] = BitVec()._init(long(not not item), 1)
+ self[index:index] = BitVec(long(not not item), 1)
def remove(self, value):
def copy(self):
- return BitVec()._init(self._data, self._len)
+ return BitVec(self._data, self._len)
def seq(self):
#rprt(`self`+'.__getslice__'+`i, j`+'\n')
i, j = _check_slice(self._len, i, j)
if i >= j:
- return BitVec()._init(0L, 0)
+ return BitVec(0L, 0)
if i:
ndata = self._data >> i
else:
#we'll have to invent faster variants here
#e.g. mod_2exp
ndata = ndata & ((1L << nlength) - 1)
- return BitVec()._init(ndata, nlength)
+ return BitVec(ndata, nlength)
def __setslice__(self, i, j, sequence, *rest):
#rprt(`self`+'.__setslice__'+`(i, j, sequence) + rest`+'\n')
if type(multiplier) != type(0):
raise TypeError, 'sequence subscript not int'
if multiplier <= 0:
- return BitVec()._init(0L, 0)
+ return BitVec(0L, 0)
elif multiplier == 1:
return self.copy()
#handle special cases all 0 or all 1...
if self._data == 0L:
- return BitVec()._init(0L, self._len * multiplier)
+ return BitVec(0L, self._len * multiplier)
elif (~self)._data == 0L:
- return ~BitVec()._init(0L, self._len * multiplier)
+ return ~BitVec(0L, self._len * multiplier)
#otherwise el cheapo again...
- retval = BitVec()._init(0L, 0)
+ retval = BitVec(0L, 0)
while multiplier:
retval, multiplier = retval + self, multiplier - 1
return retval
if type(otherseq) != type(self):
otherseq = apply(bitvec, (otherseq, ) + rest)
#sequence is now of our own type
- return BitVec()._init(self._data & otherseq._data, \
+ return BitVec(self._data & otherseq._data, \
min(self._len, otherseq._len))
if type(otherseq) != type(self):
otherseq = apply(bitvec, (otherseq, ) + rest)
#sequence is now of our own type
- return BitVec()._init(self._data ^ otherseq._data, \
+ return BitVec(self._data ^ otherseq._data, \
max(self._len, otherseq._len))
if type(otherseq) != type(self):
otherseq = apply(bitvec, (otherseq, ) + rest)
#sequence is now of our own type
- return BitVec()._init(self._data | otherseq._data, \
+ return BitVec(self._data | otherseq._data, \
max(self._len, otherseq._len))
def __invert__(self):
#rprt(`self`+'.__invert__()\n')
- return BitVec()._init(~self._data & ((1L << self._len) - 1), \
+ return BitVec(~self._data & ((1L << self._len) - 1), \
self._len)
def __coerce__(self, otherseq, *rest):
return float(self._data)
-def bitvec(params):
- return apply(BitVec().init, params)
+bitvec = BitVec