What does `return +/- ` do in python?
Question:
I was going through the CPython source code and I found the following piece of code from the standard library(ast.py
).
if isinstance(node.op, UAdd):
return + operand
else:
return - operand
I tried the following in my python interpreter
>>> def s():
... return + 1
...
>>> s()
1
But this is same as the following right?
def s():
return 1
Can any one help me to understand what does the expression return +
or return -
do in python and when we should use this?
Answers:
return + operand
is equivalent to return operand
(if operand is a number). The only purpose I see is to insist on the fact that we do not want the opposite of operand
.
Both +
and -
can be used as unary or binary operators. In your case they are used as unary operators. return + operand
is the same as return operand
. We are used to see them in the form of return +operand
or return -operand
.
plus and minus in this context are unary operators. That is, they accept a single operand. This is in comparison to the binary operator *
(for example) that operates on two operands. Evidently +1
is just 1
. So the unary operator +
in your return statement is redundant.
I haven’t studied the code, so I don’t know for sure, but Python allows overriding unary operator behavior:
__pos__(self)
Implements behavior for unary positive (e.g. +some_object)
__neg__(self)
Implements behavior for negation (e.g. -some_object)
So operand
in your case could be an object of a class which overrides those magic methods.
This means that return + operand
is NOT equivalent to return operand
.
I was going through the CPython source code and I found the following piece of code from the standard library(ast.py
).
if isinstance(node.op, UAdd):
return + operand
else:
return - operand
I tried the following in my python interpreter
>>> def s():
... return + 1
...
>>> s()
1
But this is same as the following right?
def s():
return 1
Can any one help me to understand what does the expression return +
or return -
do in python and when we should use this?
return + operand
is equivalent to return operand
(if operand is a number). The only purpose I see is to insist on the fact that we do not want the opposite of operand
.
Both +
and -
can be used as unary or binary operators. In your case they are used as unary operators. return + operand
is the same as return operand
. We are used to see them in the form of return +operand
or return -operand
.
plus and minus in this context are unary operators. That is, they accept a single operand. This is in comparison to the binary operator *
(for example) that operates on two operands. Evidently +1
is just 1
. So the unary operator +
in your return statement is redundant.
I haven’t studied the code, so I don’t know for sure, but Python allows overriding unary operator behavior:
__pos__(self)
Implements behavior for unary positive (e.g. +some_object)
__neg__(self)
Implements behavior for negation (e.g. -some_object)
So operand
in your case could be an object of a class which overrides those magic methods.
This means that return + operand
is NOT equivalent to return operand
.