numeric emulation and __pos__

Terry Reedy tjreedy at
Tue Jul 8 23:17:52 CEST 2008

Ethan Furman wrote:

> Anybody have an example of when the unary + actually does something?
> Besides the below Decimal example.  I'm curious under what circumstances
> it would be useful for more than just completeness (although
> completeness for it's own sake is important, IMO).

All true operators and some built-in functions translate to special 
method calls.
-x == x.__neg__() == type(x).__neg__(x) == x.__class__.__neg__(x)

What should happen to '+x'? Raise SyntaxError?  Ignore the '+'?  Or 
translate it to __pos__ in analogy with '-' and __neg__?  Guido made the 
third choice:  partly for consistency perhaps, but also for the benefit 
of user-written classes.  Decimal started as user-contributed

Does anything else use this hook?  I don't know, but I do know that 
there are periodic demands for *more* operators for user use.  So I 
expect it has been.


More information about the Python-list mailing list