*** empty log message ***

This commit is contained in:
Guido van Rossum 1993-12-17 14:23:52 +00:00
parent 3cbc16d904
commit 7565b93414
8 changed files with 51 additions and 49 deletions

View File

@ -4,16 +4,21 @@
from math import sqrt from math import sqrt
def complex(re, im): class complex:
return Complex().init(re, im)
def __init__(self, re, im):
class Complex:
def init(self, re, im):
self.re = float(re) self.re = float(re)
self.im = float(im) 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): def __repr__(self):
return 'complex' + `self.re, self.im` return 'complex' + `self.re, self.im`
@ -60,9 +65,16 @@ class Complex:
def test(): def test():
a = complex(2, 0) a = complex(2, 0)
b = complex(3, 4) b = complex(3, 4)
print a, b print a
print a+b, a-b, a*b, a/b print b
print b+a, b-a, b*a, b/a 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) i = complex(0, 1)
print i, i*i, i*i*i, i*i*i*i print i, i*i, i*i*i, i*i*i*i
j = complex(1, 1) j = complex(1, 1)

View File

@ -41,11 +41,11 @@ class Dbm:
def test(): def test():
d = Dbm('@dbm', 'rw', 0666) d = Dbm('@dbm', 'rw', 0600)
print d print d
while 1: while 1:
try: try:
key = eval(raw_input('key: ')) key = input('key: ')
if d.has_key(key): if d.has_key(key):
value = d[key] value = d[key]
print 'currently:', value print 'currently:', value

View File

@ -1,4 +1,4 @@
Examples of classes that implement special operators (see class.doc): Examples of classes that implement special operators (see reference manual):
Complex.py Complex numbers Complex.py Complex numbers
Dates.py Date manipulation package by Tim Peters Dates.py Date manipulation package by Tim Peters

View File

@ -16,7 +16,7 @@ def range(*a):
start, stop, step = a start, stop, step = a
else: else:
raise TypeError, 'range() needs 1-3 arguments' raise TypeError, 'range() needs 1-3 arguments'
return Range().init(start, stop, step) return Range(start, stop, step)
# Class implementing a range object. # Class implementing a range object.
@ -26,14 +26,13 @@ def range(*a):
class Range: class Range:
# initialization -- should be called only by range() above # initialization -- should be called only by range() above
def init(self, start, stop, step): def __init__(self, start, stop, step):
if step == 0: if step == 0:
raise ValueError, 'range() called with zero step' raise ValueError, 'range() called with zero step'
self.start = start self.start = start
self.stop = stop self.stop = stop
self.step = step self.step = step
self.len = max(0, int((self.stop - self.start) / self.step)) self.len = max(0, int((self.stop - self.start) / self.step))
return self
# implement `x` and is also used by print x # implement `x` and is also used by print x
def __repr__(self): def __repr__(self):

View File

@ -2,7 +2,7 @@
def rat(num, den): def rat(num, den):
return Rat().init(num, den) return Rat(num, den)
def gcd(a, b): def gcd(a, b):
@ -13,13 +13,12 @@ def gcd(a, b):
class Rat: class Rat:
def init(self, num, den): def __init__(self, num, den):
if den == 0: if den == 0:
raise ZeroDivisionError, 'rat(x, 0)' raise ZeroDivisionError, 'rat(x, 0)'
g = gcd(num, den) g = gcd(num, den)
self.num = num/g self.num = num/g
self.den = den/g self.den = den/g
return self
def __repr__(self): def __repr__(self):
return 'rat' + `self.num, self.den` return 'rat' + `self.num, self.den`
@ -49,6 +48,8 @@ class Rat:
return a, rat(b, 1L) return a, rat(b, 1L)
if t == type(0.0): if t == type(0.0):
return a.__float__(), b return a.__float__(), b
if t == type(a) and a.__class__ == b.__class__:
return a, b
raise TypeError, 'Rat.__coerce__: bad other arg' raise TypeError, 'Rat.__coerce__: bad other arg'
def __add__(a, b): def __add__(a, b):
@ -93,4 +94,4 @@ def test():
except ZeroDivisionError: except ZeroDivisionError:
print 'OK' print 'OK'
#test() test()

View File

@ -8,7 +8,7 @@
# ... # ...
# !dlroW olleH # !dlroW olleH
# #
# The .forw is so you can use anonymous sequences in init, and still # The .forw is so you can use anonymous sequences in __init__, and still
# keep a reference the forward sequence. ) # keep a reference the forward sequence. )
# If you give it a non-anonymous mutable sequence, the reverse sequence # If you give it a non-anonymous mutable sequence, the reverse sequence
# will track the updated values. ( but not reassignment! - another # will track the updated values. ( but not reassignment! - another

View File

@ -2,16 +2,15 @@
def vec(*v): def vec(*v):
return apply(Vec().init, v) return apply(Vec, v)
class Vec: class Vec:
def init(self, *v): def __init__(self, *v):
self.v = [] self.v = []
for x in v: for x in v:
self.v.append(x) self.v.append(x)
return self
def fromlist(self, v): def fromlist(self, v):

View File

@ -48,7 +48,7 @@ def _check_slice(len, i, j):
class BitVec: class BitVec:
def init(self, *params): def __init__(self, *params):
self._data = 0L self._data = 0L
self._len = 0 self._len = 0
if not len(params): if not len(params):
@ -93,20 +93,12 @@ class BitVec:
else: else:
raise error, 'bitvec() requires 0 -- 2 parameter(s)' 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): def append(self, item):
#_check_value(item) #_check_value(item)
#self[self._len:self._len] = [item] #self[self._len:self._len] = [item]
self[self._len:self._len] = \ self[self._len:self._len] = \
BitVec()._init(long(not not item), 1) BitVec(long(not not item), 1)
def count(self, value): def count(self, value):
@ -138,7 +130,7 @@ class BitVec:
def insert(self, index, item): def insert(self, index, item):
#_check_value(item) #_check_value(item)
#self[index:index] = [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 remove(self, value):
@ -163,7 +155,7 @@ class BitVec:
def copy(self): def copy(self):
return BitVec()._init(self._data, self._len) return BitVec(self._data, self._len)
def seq(self): def seq(self):
@ -229,7 +221,7 @@ class BitVec:
#rprt(`self`+'.__getslice__'+`i, j`+'\n') #rprt(`self`+'.__getslice__'+`i, j`+'\n')
i, j = _check_slice(self._len, i, j) i, j = _check_slice(self._len, i, j)
if i >= j: if i >= j:
return BitVec()._init(0L, 0) return BitVec(0L, 0)
if i: if i:
ndata = self._data >> i ndata = self._data >> i
else: else:
@ -239,7 +231,7 @@ class BitVec:
#we'll have to invent faster variants here #we'll have to invent faster variants here
#e.g. mod_2exp #e.g. mod_2exp
ndata = ndata & ((1L << nlength) - 1) ndata = ndata & ((1L << nlength) - 1)
return BitVec()._init(ndata, nlength) return BitVec(ndata, nlength)
def __setslice__(self, i, j, sequence, *rest): def __setslice__(self, i, j, sequence, *rest):
#rprt(`self`+'.__setslice__'+`(i, j, sequence) + rest`+'\n') #rprt(`self`+'.__setslice__'+`(i, j, sequence) + rest`+'\n')
@ -274,16 +266,16 @@ class BitVec:
if type(multiplier) != type(0): if type(multiplier) != type(0):
raise TypeError, 'sequence subscript not int' raise TypeError, 'sequence subscript not int'
if multiplier <= 0: if multiplier <= 0:
return BitVec()._init(0L, 0) return BitVec(0L, 0)
elif multiplier == 1: elif multiplier == 1:
return self.copy() return self.copy()
#handle special cases all 0 or all 1... #handle special cases all 0 or all 1...
if self._data == 0L: if self._data == 0L:
return BitVec()._init(0L, self._len * multiplier) return BitVec(0L, self._len * multiplier)
elif (~self)._data == 0L: elif (~self)._data == 0L:
return ~BitVec()._init(0L, self._len * multiplier) return ~BitVec(0L, self._len * multiplier)
#otherwise el cheapo again... #otherwise el cheapo again...
retval = BitVec()._init(0L, 0) retval = BitVec(0L, 0)
while multiplier: while multiplier:
retval, multiplier = retval + self, multiplier - 1 retval, multiplier = retval + self, multiplier - 1
return retval return retval
@ -293,7 +285,7 @@ class BitVec:
if type(otherseq) != type(self): if type(otherseq) != type(self):
otherseq = apply(bitvec, (otherseq, ) + rest) otherseq = apply(bitvec, (otherseq, ) + rest)
#sequence is now of our own type #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)) min(self._len, otherseq._len))
@ -302,7 +294,7 @@ class BitVec:
if type(otherseq) != type(self): if type(otherseq) != type(self):
otherseq = apply(bitvec, (otherseq, ) + rest) otherseq = apply(bitvec, (otherseq, ) + rest)
#sequence is now of our own type #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)) max(self._len, otherseq._len))
@ -311,13 +303,13 @@ class BitVec:
if type(otherseq) != type(self): if type(otherseq) != type(self):
otherseq = apply(bitvec, (otherseq, ) + rest) otherseq = apply(bitvec, (otherseq, ) + rest)
#sequence is now of our own type #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)) max(self._len, otherseq._len))
def __invert__(self): def __invert__(self):
#rprt(`self`+'.__invert__()\n') #rprt(`self`+'.__invert__()\n')
return BitVec()._init(~self._data & ((1L << self._len) - 1), \ return BitVec(~self._data & ((1L << self._len) - 1), \
self._len) self._len)
def __coerce__(self, otherseq, *rest): def __coerce__(self, otherseq, *rest):
@ -337,5 +329,4 @@ class BitVec:
return float(self._data) return float(self._data)
def bitvec(params): bitvec = BitVec
return apply(BitVec().init, params)