numeric emulation and __pos__

Ethan Furman ethan at stoneleaf.us
Tue Jul 8 18:54:37 EDT 2008


Terry Reedy wrote:
> 
> 
> 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 decimal.py.
> 
> 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.
> 
> tjr

It definitely makes sense from that perspective.  As I was reading the 
docs for numeric emulation I came across the unary + and wasn't sure 
what to make of it.  I tried it out, and got exactly what I put in, for 
everything I tried.  So I wondered if there were any situations where 
you would get something besides what you started with.

Thanks to everyone for your comments and help.
--
Ethan



More information about the Python-list mailing list