Python has a unary negation operator to negate numbers, as you're probably well aware:
>>> x = 5
>>> print(-x)
-5
>>> x = -5
>>> print(-x)
5
Say you're making a list of numbers, though. It might be more consistent if you prefixed the positive ones with a +
, so Python has a unary +
operator, too:
>>> numbers = [-3, -2, -1, 0, +1, +2, +3]
When you use the unary +
operator on a number, you're right that it doesn't do anything; it's there just for consistency.
Now when you consider that in Python you can override operators on types, of course you'd need a __neg__
to negate an instance of that type. Python just decided to be consistent by also having a __pos__
to…not negate an instance of that type. float
, like all other types overriding these operators, follow this protocol, and float
's implementation of __pos__
is just the identity function.