Mini-PEP: Simplifying numbers.py

* Convert binary methods in Integral to mixin methods
* Remove three-arg __pow__ as a required method
* Make __int__ the root method instead of __long__.
This commit is contained in:
Raymond Hettinger 2008-06-11 00:25:29 +00:00
parent 31296c09ed
commit caea65e465

View file

@ -283,87 +283,54 @@ class Rational(Real):
class Integral(Rational): class Integral(Rational):
"""Integral adds a conversion to long and the bit-string operations.""" """Integral adds a conversion to int and the bit-string operations."""
@abstractmethod @abstractmethod
def __long__(self): def __int__(self):
"""long(self)""" """int(self)"""
raise NotImplementedError raise NotImplementedError
def __index__(self): def __index__(self):
"""index(self)""" """index(self)"""
return long(self) return int(self)
@abstractmethod
def __pow__(self, exponent, modulus=None):
"""self ** exponent % modulus, but maybe faster.
Accept the modulus argument if you want to support the
3-argument version of pow(). Raise a TypeError if exponent < 0
or any argument isn't Integral. Otherwise, just implement the
2-argument version described in Complex.
"""
raise NotImplementedError
@abstractmethod
def __lshift__(self, other): def __lshift__(self, other):
"""self << other""" return int(self) << int(other)
raise NotImplementedError
@abstractmethod
def __rlshift__(self, other): def __rlshift__(self, other):
"""other << self""" return int(other) << int(self)
raise NotImplementedError
@abstractmethod
def __rshift__(self, other): def __rshift__(self, other):
"""self >> other""" return int(self) >> int(other)
raise NotImplementedError
@abstractmethod
def __rrshift__(self, other): def __rrshift__(self, other):
"""other >> self""" return int(other) >> int(self)
raise NotImplementedError
@abstractmethod
def __and__(self, other): def __and__(self, other):
"""self & other""" return int(self) & int(other)
raise NotImplementedError
@abstractmethod
def __rand__(self, other): def __rand__(self, other):
"""other & self""" return int(other) & int(self)
raise NotImplementedError
@abstractmethod
def __xor__(self, other): def __xor__(self, other):
"""self ^ other""" return int(self) ^ int(other)
raise NotImplementedError
@abstractmethod
def __rxor__(self, other): def __rxor__(self, other):
"""other ^ self""" return int(other) ^ int(self)
raise NotImplementedError
@abstractmethod
def __or__(self, other): def __or__(self, other):
"""self | other""" return int(self) | int(other)
raise NotImplementedError
@abstractmethod
def __ror__(self, other): def __ror__(self, other):
"""other | self""" return int(other) | int(self)
raise NotImplementedError
@abstractmethod
def __invert__(self): def __invert__(self):
"""~self""" return ~int(self)
raise NotImplementedError
# Concrete implementations of Rational and Real abstract methods. # Concrete implementations of Rational and Real abstract methods.
def __float__(self): def __float__(self):
"""float(self) == float(long(self))""" """float(self) == float(int(self))"""
return float(long(self)) return float(int(self))
@property @property
def numerator(self): def numerator(self):