0

The reference says

The unary + (plus) operator yields its numeric argument unchanged.

It doesn't convert types in comparison to, say, JavaScript. So what is the case when I need to use this operator instead of just passing the unchanged argument?

enkryptor
  • 1,574
  • 1
  • 17
  • 27
  • 2
    @Kaya3 That question and its accepted answer are about the `Decimal` module, not Python in general. – Barmar Mar 18 '21 at 20:38
  • 1
    @Barmar The accepted answer says: *"What that plus sign does depends on what it's defined to do by the result of that expression (that object's __pos__() method is called)."* I think that is clearly the correct answer to this question. – kaya3 Mar 18 '21 at 20:39
  • Most languages have this operator, and I think it's just for consistency with the `-` operator, which returns the number with opposite sign. – Barmar Mar 18 '21 at 20:39
  • @kaya3 That explains how it works, but not *why* it exists in the first place. It almost certainly predates the `Decimal` module. – Barmar Mar 18 '21 at 20:40
  • It's also consistent with the way numbers are written. You can write `+123` or you can write `num = 123; +num` – Barmar Mar 18 '21 at 20:41
  • 2
    I would think that an explanation of what it does (in particular, that what it does is defined by each class for its own objects) should sufficiently answer "when you need to use it". This question doesn't ask for "why" Python has the operator at all, but if you interpret it that way then [this other question](https://stackoverflow.com/q/16819023/12299000) would be an appropriate dupe target instead. Either way, it's definitely a dupe. – kaya3 Mar 18 '21 at 20:48

0 Answers0