numeric emulation and __pos__

casevh casevh at
Wed Jul 9 05:41:41 CEST 2008

On Jul 7, 4:12 pm, Ethan Furman <et... at> wrote:
> Greetings, List!
> I'm working on a numeric data type for measured values that will keep
> track of and limit results to the number of significant digits
> originally defined for the values in question.
> I am doing this primarily because I enjoy playing with numbers, and also
> to get some experience with unit testing.
> At this point I have the __init__ portion finished, and am starting on
> the various operator functions.
> Questions for the group:
> 1) Any reason to support the less common operators?
>         i.e. <<, >>, &, ^, |
Assuming you are working with decimal numbers, the &, ^, | may not be
of any use for your application. The shift operators may be useful but
there are two possible ways to define their behavior:

1) Multiplication or division by powers of 2. This mimics the common
use of those operators as used with binary numbers.

2) Multiplication or division by powers of 10.

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

The unary + operator is frequently added for symmetry with -, however
it is also used to force an existing number to match a new precision
setting. For example, using the decimal module:

>>> from decimal import *
>>> t=Decimal('1.23456')
>>> t
>>> getcontext().prec = 5
>>> +t

> Thanks for the feedback.
> --
> Ethan


More information about the Python-list mailing list