[英]What does the operator += return in Python
While I was trying to answer another person's question on stackoverflow about the difference between =
and +=
in Python, I encountered the following problem:当我试图在stackoverflow上回答另一个人关于Python中
=
和+=
之间的区别的问题时,我遇到了以下问题:
class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
print(a+=b)
The last print
call was not successful and gave me this:最后一次
print
电话不成功,给了我这个:
File "<ipython-input-8-0faa82ba9e4a>", line 3
print(a+=b)
^
SyntaxError: invalid syntax
I don't know why this isn't working.我不知道为什么这不起作用。 Maybe it has something to do with the keyword argument passing mechanism?
也许它与关键字参数传递机制有关? I just can't find any resource on this topic, since the overloaded
__iadd__
method already returns a Foo
object.我只是找不到关于这个主题的任何资源,因为重载的
__iadd__
方法已经返回了一个Foo
object。
************** update ****************** ************** 更新 ******************
If I change the __iadd__
method like this (just remove the return
statement):如果我像这样更改
__iadd__
方法(只需删除return
语句):
...
def __iadd__(self, that):
print("__iadd__ was called")
self.value = self.value + that.value
a = Foo(1, 'alice')
b = Foo(2, 'bob')
a += b
print(a) # Outputs: None
So, the final return
statement in __iadd__
is indeed required.因此,确实需要
__iadd__
中的最终return
语句。 But it does not function as I thought (I come from a C/C++ background, so this behavior is a bit strange for me)但它不像我想的那样 function (我来自 C/C++ 背景,所以这种行为对我来说有点奇怪)
************************* 2nd Update ******************************** ************************* 第二次更新 ************************* *********
I almost forget that =
in Python makes up a statement instead of an expression.我几乎忘记了 Python 中的
=
构成了一个语句而不是一个表达式。 The return
statement in __iadd__
and my experience with other languages gives me an illusion that +=
could be used as an expression. __iadd__
中的return
语句以及我使用其他语言的经验让我产生了一种错觉,即+=
可以用作表达式。
As stated in the Python documentation, __add__
is used to construct a new object.如 Python 文档中所述,
__add__
用于构造新的 object。 __iadd__
is designed for inplace modifications. __iadd__
专为就地修改而设计。 But it all depends on the implementation.但这一切都取决于实施。 Although
__iadd__
returns a object, this object is somehow "intercepted" by the language and reassigned to the left-hand operator, and, the final effect is, __iadd__
remains a statement, not an expression.尽管
__iadd__
返回 object,但此 object 以某种方式被语言“拦截”并重新分配给左侧运算符,最终结果是, __iadd__
仍然是语句,而不是表达式。 Please correct me if I'm wrong.如果我错了,请纠正我。 Also, I didn't find any resource confirming that
+=
is a statement.此外,我没有找到任何资源确认
+=
是一个声明。
a = 1
is an assignment statement.a = 1
是一个赋值语句。 It's not allowed to be used as a function argument.print('Hello world', end='')
still works.print('Hello world', end='')
仍然有效。x = x + y
is equivalent to x = x.__add__(y)
, x = x + y
等价于x = x.__add__(y)
,x += y
is equivalent to x = x.__iadd__(y)
, check the doc for more details. x += y
等效于x = x.__iadd__(y)
,请查看文档以获取更多详细信息。class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
c = a + b # objects a and b are unchanged
a += b # object a is changed
print(c) # name: default, value: 3
print(a) # name: alice, value: 3
While I was trying to answer another person's question on stackoverflow about the difference between =
and +=
in Python, I encountered the following problem:当我试图在stackoverflow上回答另一个人关于Python中
=
和+=
之间的区别的问题时,我遇到了以下问题:
class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
print(a+=b)
The last print
call was not successful and gave me this:最后一次
print
电话不成功,给了我这个:
File "<ipython-input-8-0faa82ba9e4a>", line 3
print(a+=b)
^
SyntaxError: invalid syntax
I don't know why this isn't working.我不知道为什么这不起作用。 Maybe it has something to do with the keyword argument passing mechanism?
也许它与关键字参数传递机制有关? I just can't find any resource on this topic, since the overloaded
__iadd__
method already returns a Foo
object.我只是找不到关于这个主题的任何资源,因为重载的
__iadd__
方法已经返回了一个Foo
object。
************** update ****************** ************** 更新 ******************
If I change the __iadd__
method like this (just remove the return
statement):如果我像这样更改
__iadd__
方法(只需删除return
语句):
...
def __iadd__(self, that):
print("__iadd__ was called")
self.value = self.value + that.value
a = Foo(1, 'alice')
b = Foo(2, 'bob')
a += b
print(a) # Outputs: None
So, the final return
statement in __iadd__
is indeed required.因此,确实需要
__iadd__
中的最终return
语句。 But it does not function as I thought (I come from a C/C++ background, so this behavior is a bit strange for me)但它不像我想的那样 function (我来自 C/C++ 背景,所以这种行为对我来说有点奇怪)
************************* 2nd Update ******************************** ************************* 第二次更新 ************************* *********
I almost forget that =
in Python makes up a statement instead of an expression.我几乎忘记了 Python 中的
=
构成了一个语句而不是一个表达式。 The return
statement in __iadd__
and my experience with other languages gives me an illusion that +=
could be used as an expression. __iadd__
中的return
语句以及我使用其他语言的经验让我产生了一种错觉,即+=
可以用作表达式。
As stated in the Python documentation, __add__
is used to construct a new object.如 Python 文档中所述,
__add__
用于构造新的 object。 __iadd__
is designed for inplace modifications. __iadd__
专为就地修改而设计。 But it all depends on the implementation.但这一切都取决于实施。 Although
__iadd__
returns a object, this object is somehow "intercepted" by the language and reassigned to the left-hand operator, and, the final effect is, __iadd__
remains a statement, not an expression.尽管
__iadd__
返回 object,但这个 object 以某种方式被语言“拦截”并重新分配给左手运算符,最终结果是, __iadd__
仍然是语句,而不是表达式。 Please correct me if I'm wrong.如果我错了,请纠正我。 Also, I didn't find any resource confirming that
+=
is a statement.此外,我没有找到任何资源确认
+=
是一个声明。
a = 1
is an assignment statement.a = 1
是一个赋值语句。 It's not allowed to be used as a function argument.print('Hello world', end='')
still works.print('Hello world', end='')
仍然有效。x = x + y
is equivalent to x = x.__add__(y)
, x = x + y
等价于x = x.__add__(y)
,x += y
is equivalent to x = x.__iadd__(y)
, check the doc for more details. x += y
等效于x = x.__iadd__(y)
,请查看文档以获取更多详细信息。class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
c = a + b # objects a and b are unchanged
a += b # object a is changed
print(c) # name: default, value: 3
print(a) # name: alice, value: 3
While I was trying to answer another person's question on stackoverflow about the difference between =
and +=
in Python, I encountered the following problem:当我试图在stackoverflow上回答另一个人关于Python中
=
和+=
之间的区别的问题时,我遇到了以下问题:
class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
print(a+=b)
The last print
call was not successful and gave me this:最后一次
print
电话不成功,给了我这个:
File "<ipython-input-8-0faa82ba9e4a>", line 3
print(a+=b)
^
SyntaxError: invalid syntax
I don't know why this isn't working.我不知道为什么这不起作用。 Maybe it has something to do with the keyword argument passing mechanism?
也许它与关键字参数传递机制有关? I just can't find any resource on this topic, since the overloaded
__iadd__
method already returns a Foo
object.我只是找不到关于这个主题的任何资源,因为重载的
__iadd__
方法已经返回了一个Foo
object。
************** update ****************** ************** 更新 ******************
If I change the __iadd__
method like this (just remove the return
statement):如果我像这样更改
__iadd__
方法(只需删除return
语句):
...
def __iadd__(self, that):
print("__iadd__ was called")
self.value = self.value + that.value
a = Foo(1, 'alice')
b = Foo(2, 'bob')
a += b
print(a) # Outputs: None
So, the final return
statement in __iadd__
is indeed required.因此,确实需要
__iadd__
中的最终return
语句。 But it does not function as I thought (I come from a C/C++ background, so this behavior is a bit strange for me)但它不像我想的那样 function (我来自 C/C++ 背景,所以这种行为对我来说有点奇怪)
************************* 2nd Update ******************************** ************************* 第二次更新 ************************* *********
I almost forget that =
in Python makes up a statement instead of an expression.我几乎忘记了 Python 中的
=
构成了一个语句而不是一个表达式。 The return
statement in __iadd__
and my experience with other languages gives me an illusion that +=
could be used as an expression. __iadd__
中的return
语句以及我使用其他语言的经验让我产生了一种错觉,即+=
可以用作表达式。
As stated in the Python documentation, __add__
is used to construct a new object.如 Python 文档中所述,
__add__
用于构造新的 object。 __iadd__
is designed for inplace modifications. __iadd__
专为就地修改而设计。 But it all depends on the implementation.但这一切都取决于实施。 Although
__iadd__
returns a object, this object is somehow "intercepted" by the language and reassigned to the left-hand operator, and, the final effect is, __iadd__
remains a statement, not an expression.尽管
__iadd__
返回 object,但这个 object 以某种方式被语言“拦截”并重新分配给左手运算符,最终结果是, __iadd__
仍然是语句,而不是表达式。 Please correct me if I'm wrong.如果我错了,请纠正我。 Also, I didn't find any resource confirming that
+=
is a statement.此外,我没有找到任何资源确认
+=
是一个声明。
a = 1
is an assignment statement.a = 1
是一个赋值语句。 It's not allowed to be used as a function argument.print('Hello world', end='')
still works.print('Hello world', end='')
仍然有效。x = x + y
is equivalent to x = x.__add__(y)
, x = x + y
等价于x = x.__add__(y)
,x += y
is equivalent to x = x.__iadd__(y)
, check the doc for more details. x += y
等效于x = x.__iadd__(y)
,请查看文档以获取更多详细信息。class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
c = a + b # objects a and b are unchanged
a += b # object a is changed
print(c) # name: default, value: 3
print(a) # name: alice, value: 3
While I was trying to answer another person's question on stackoverflow about the difference between =
and +=
in Python, I encountered the following problem:当我试图在stackoverflow上回答另一个人关于Python中
=
和+=
之间的区别的问题时,我遇到了以下问题:
class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
print(a+=b)
The last print
call was not successful and gave me this:最后一次
print
电话不成功,给了我这个:
File "<ipython-input-8-0faa82ba9e4a>", line 3
print(a+=b)
^
SyntaxError: invalid syntax
I don't know why this isn't working.我不知道为什么这不起作用。 Maybe it has something to do with the keyword argument passing mechanism?
也许它与关键字参数传递机制有关? I just can't find any resource on this topic, since the overloaded
__iadd__
method already returns a Foo
object.我只是找不到关于这个主题的任何资源,因为重载的
__iadd__
方法已经返回了一个Foo
object。
************** update ****************** ************** 更新 ******************
If I change the __iadd__
method like this (just remove the return
statement):如果我像这样更改
__iadd__
方法(只需删除return
语句):
...
def __iadd__(self, that):
print("__iadd__ was called")
self.value = self.value + that.value
a = Foo(1, 'alice')
b = Foo(2, 'bob')
a += b
print(a) # Outputs: None
So, the final return
statement in __iadd__
is indeed required.因此,确实需要
__iadd__
中的最终return
语句。 But it does not function as I thought (I come from a C/C++ background, so this behavior is a bit strange for me)但它不像我想的那样 function (我来自 C/C++ 背景,所以这种行为对我来说有点奇怪)
************************* 2nd Update ******************************** ************************* 第二次更新 ************************* *********
I almost forget that =
in Python makes up a statement instead of an expression.我几乎忘记了 Python 中的
=
构成了一个语句而不是一个表达式。 The return
statement in __iadd__
and my experience with other languages gives me an illusion that +=
could be used as an expression. __iadd__
中的return
语句以及我使用其他语言的经验让我产生了一种错觉,即+=
可以用作表达式。
As stated in the Python documentation, __add__
is used to construct a new object.如 Python 文档中所述,
__add__
用于构造新的 object。 __iadd__
is designed for inplace modifications. __iadd__
专为就地修改而设计。 But it all depends on the implementation.但这一切都取决于实施。 Although
__iadd__
returns a object, this object is somehow "intercepted" by the language and reassigned to the left-hand operator, and, the final effect is, __iadd__
remains a statement, not an expression.尽管
__iadd__
返回 object,但这个 object 以某种方式被语言“拦截”并重新分配给左手运算符,最终结果是, __iadd__
仍然是语句,而不是表达式。 Please correct me if I'm wrong.如果我错了,请纠正我。 Also, I didn't find any resource confirming that
+=
is a statement.此外,我没有找到任何资源确认
+=
是一个声明。
a = 1
is an assignment statement.a = 1
是一个赋值语句。 It's not allowed to be used as a function argument.print('Hello world', end='')
still works.print('Hello world', end='')
仍然有效。x = x + y
is equivalent to x = x.__add__(y)
, x = x + y
等价于x = x.__add__(y)
,x += y
is equivalent to x = x.__iadd__(y)
, check the doc for more details. x += y
等效于x = x.__iadd__(y)
,请查看文档以获取更多详细信息。class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
c = a + b # objects a and b are unchanged
a += b # object a is changed
print(c) # name: default, value: 3
print(a) # name: alice, value: 3
While I was trying to answer another person's question on stackoverflow about the difference between =
and +=
in Python, I encountered the following problem:当我试图在stackoverflow上回答另一个人关于Python中
=
和+=
之间的区别的问题时,我遇到了以下问题:
class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
print(a+=b)
The last print
call was not successful and gave me this:最后一次
print
电话不成功,给了我这个:
File "<ipython-input-8-0faa82ba9e4a>", line 3
print(a+=b)
^
SyntaxError: invalid syntax
I don't know why this isn't working.我不知道为什么这不起作用。 Maybe it has something to do with the keyword argument passing mechanism?
也许它与关键字参数传递机制有关? I just can't find any resource on this topic, since the overloaded
__iadd__
method already returns a Foo
object.我只是找不到关于这个主题的任何资源,因为重载的
__iadd__
方法已经返回了一个Foo
object。
************** update ****************** ************** 更新 ******************
If I change the __iadd__
method like this (just remove the return
statement):如果我像这样更改
__iadd__
方法(只需删除return
语句):
...
def __iadd__(self, that):
print("__iadd__ was called")
self.value = self.value + that.value
a = Foo(1, 'alice')
b = Foo(2, 'bob')
a += b
print(a) # Outputs: None
So, the final return
statement in __iadd__
is indeed required.因此,确实需要
__iadd__
中的最终return
语句。 But it does not function as I thought (I come from a C/C++ background, so this behavior is a bit strange for me)但它不像我想的那样 function (我来自 C/C++ 背景,所以这种行为对我来说有点奇怪)
************************* 2nd Update ******************************** ************************* 第二次更新 ************************* *********
I almost forget that =
in Python makes up a statement instead of an expression.我几乎忘记了 Python 中的
=
构成了一个语句而不是一个表达式。 The return
statement in __iadd__
and my experience with other languages gives me an illusion that +=
could be used as an expression. __iadd__
中的return
语句以及我使用其他语言的经验让我产生了一种错觉,即+=
可以用作表达式。
As stated in the Python documentation, __add__
is used to construct a new object.如 Python 文档中所述,
__add__
用于构造新的 object。 __iadd__
is designed for inplace modifications. __iadd__
专为就地修改而设计。 But it all depends on the implementation.但这一切都取决于实施。 Although
__iadd__
returns a object, this object is somehow "intercepted" by the language and reassigned to the left-hand operator, and, the final effect is, __iadd__
remains a statement, not an expression.尽管
__iadd__
返回 object,但这个 object 以某种方式被语言“拦截”并重新分配给左手运算符,最终结果是, __iadd__
仍然是语句,而不是表达式。 Please correct me if I'm wrong.如果我错了,请纠正我。 Also, I didn't find any resource confirming that
+=
is a statement.此外,我没有找到任何资源确认
+=
是一个声明。
a = 1
is an assignment statement.a = 1
是一个赋值语句。 It's not allowed to be used as a function argument.print('Hello world', end='')
still works.print('Hello world', end='')
仍然有效。x = x + y
is equivalent to x = x.__add__(y)
, x = x + y
等价于x = x.__add__(y)
,x += y
is equivalent to x = x.__iadd__(y)
, check the doc for more details. x += y
等效于x = x.__iadd__(y)
,请查看文档以获取更多详细信息。class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
c = a + b # objects a and b are unchanged
a += b # object a is changed
print(c) # name: default, value: 3
print(a) # name: alice, value: 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.