Unary plus operator and __pos__

Gerrit gerrit at nl.linux.org
Tue Sep 28 03:37:51 EDT 2004


Erik Max Francis wrote:
> Carlos Ribeiro wrote:
> 
> > Now I'm curious about the operator itself. Why is  the unary plus
> > operator associated with the __pos__ magic method? I' can't see a
> > relation here, and I could not find much info (although I haven't
> > really looked very hard :-)
> > 
> > Anyone knows why is it so?
> 
> Unary plus is __pos__ for positive, unary minus is __neg__ for negative.
> 
> The unary plus operator, I'm sure, is a holdover from C.  Since the
> unary plus operator is a no-op for normal numeric values, I would be
> hesitant to use it to mean something else;

The Decimal class does:

>>> n = Decimal((0, (0, ), 'N'))
>>> n
Decimal("sNaN")
>>> +n
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.4/decimal.py", line 863, in __pos__
    ans = self._check_nans(context=context)
  File "/usr/local/lib/python2.4/decimal.py", line 619, in _check_nans
    1, self)
  File "/usr/local/lib/python2.4/decimal.py", line 2243, in _raise_error
    raise error, explanation
decimal.InvalidOperation: sNaN

yours,
Gerrit.

-- 
Weather in Twenthe, Netherlands 28/09 08:55:
	15.0°C mist overcast wind 4.0 m/s SW (57 m above NAP)
-- 
In the councils of government, we must guard against the acquisition of
unwarranted influence, whether sought or unsought, by the
military-industrial complex. The potential for the disastrous rise of
misplaced power exists and will persist.
    -Dwight David Eisenhower, January 17, 1961



More information about the Python-list mailing list