numeric emulation and __pos__

Mark Dickinson dickinsm at gmail.com
Tue Jul 8 09:55:26 EDT 2008


On Jul 8, 12:12 am, Ethan Furman <et... at stoneleaf.us> wrote:
> 1) Any reason to support the less common operators?
>         i.e. <<, >>, &, ^, |

No reason to support any of these for a nonintegral
nonbinary type, as far as I can see.

> 2) What, exactly, does .__pos__() do?  An example would help, too.
>

Very little:  it implements the unary + operator, which
for most numeric types is a no-op:

>>> +5.0
5.0

So you could safely implement it as:

def __pos__(self):
    return self

By the way, have you met the Decimal type?  It also
keeps track of significant zeros.  For example:

>>> from decimal import Decimal
>>> Decimal('1.95') + Decimal('2.05')
Decimal("4.00")

(Note that the output includes the extra zeros...)
Interestingly, in Decimal the __pos__ operation isn't a no-op:
it rounds a Decimal to the current context, and it also
(mistakenly, in my opinion) changes -0.0 to 0.0:

>>> +Decimal('-0.0')
Decimal("0.0")
>>> +Decimal('123456789123456789123456789123456789')
Decimal("1.234567891234567891234567891E+35")

Mark



More information about the Python-list mailing list