3

In PEP 3141 I noticed a peculiar fragment which I don't fully understand. In class Real functions real() and conjugate() return +self, like so:

@property
def real(self):
    return +self

Why is there a + in front of self? What does it do? Or is it just some kind of a typo..?

alex
  • 10,900
  • 15
  • 70
  • 100
  • 1
    Unary `+` operator: https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations I think it's just asserting that `self` is numeric. – Samwise Sep 07 '20 at 16:41
  • 1
    It's a unary plus, it invokes [`__pos__`](https://docs.python.org/3/reference/datamodel.html#object.__pos__). – jonrsharpe Sep 07 '20 at 16:41
  • 1
    My guess would be that that's some sneaky way to convert the `Real` `self` to a `float` or other "normal" numeric type. – tobias_k Sep 07 '20 at 16:42
  • So since `__pos__()` implemented in super-class raises `NotImplementedError`, is it a way to force overriding it in the sub-class just to make `real()` and `conjugate()` usable? – alex Sep 07 '20 at 16:49

0 Answers0