[Python-checkins] r64098 - python/trunk/Lib/numbers.py

Guido van Rossum guido at python.org
Wed Jun 11 06:19:31 CEST 2008


I expect we'll regret this at some point in the future. The section
"Implementing the arithmetic operations" in PEP 3141 shows elaborate
code for how to write binary operators carefully so as to allow the
right operand to override the operation if the left operand inherits
the operation from the ABC. The code here defeats that approach -- if
the left operand inherits from the ABC (not using virtual inheritance)
then it wins, regardless of what the right operand might have
preferred. We don't need the full version of that section in the PEP
(which worries about operations that are defined for  Real and Complex
too) but I expect at some point we'll want to put some of the subtlety
back in. I'm okay if that happens post beta 1 though.

--Guido

On Tue, Jun 10, 2008 at 5:25 PM, raymond.hettinger
<python-checkins at python.org> wrote:
> Author: raymond.hettinger
> Date: Wed Jun 11 02:25:29 2008
> New Revision: 64098
>
> Log:
> 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__.
>
>
>
> Modified:
>   python/trunk/Lib/numbers.py
>
> Modified: python/trunk/Lib/numbers.py
> ==============================================================================
> --- python/trunk/Lib/numbers.py (original)
> +++ python/trunk/Lib/numbers.py Wed Jun 11 02:25:29 2008
> @@ -283,87 +283,54 @@
>
>
>  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
> -    def __long__(self):
> -        """long(self)"""
> +    def __int__(self):
> +        """int(self)"""
>         raise NotImplementedError
>
>     def __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):
> -        """self << other"""
> -        raise NotImplementedError
> +        return int(self) << int(other)
>
> -    @abstractmethod
>     def __rlshift__(self, other):
> -        """other << self"""
> -        raise NotImplementedError
> +        return int(other) << int(self)
>
> -    @abstractmethod
>     def __rshift__(self, other):
> -        """self >> other"""
> -        raise NotImplementedError
> +        return int(self) >> int(other)
>
> -    @abstractmethod
>     def __rrshift__(self, other):
> -        """other >> self"""
> -        raise NotImplementedError
> +        return int(other) >> int(self)
>
> -    @abstractmethod
>     def __and__(self, other):
> -        """self & other"""
> -        raise NotImplementedError
> +        return int(self) & int(other)
>
> -    @abstractmethod
>     def __rand__(self, other):
> -        """other & self"""
> -        raise NotImplementedError
> +        return int(other) & int(self)
>
> -    @abstractmethod
>     def __xor__(self, other):
> -        """self ^ other"""
> -        raise NotImplementedError
> +        return int(self) ^ int(other)
>
> -    @abstractmethod
>     def __rxor__(self, other):
> -        """other ^ self"""
> -        raise NotImplementedError
> +        return int(other) ^ int(self)
>
> -    @abstractmethod
>     def __or__(self, other):
> -        """self | other"""
> -        raise NotImplementedError
> +        return int(self) | int(other)
>
> -    @abstractmethod
>     def __ror__(self, other):
> -        """other | self"""
> -        raise NotImplementedError
> +        return int(other) | int(self)
>
> -    @abstractmethod
>     def __invert__(self):
> -        """~self"""
> -        raise NotImplementedError
> +        return ~int(self)
>
>     # Concrete implementations of Rational and Real abstract methods.
>     def __float__(self):
> -        """float(self) == float(long(self))"""
> -        return float(long(self))
> +        """float(self) == float(int(self))"""
> +        return float(int(self))
>
>     @property
>     def numerator(self):
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
>



-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-checkins mailing list