【问题标题】:What does += mean [duplicate]+= 是什么意思[重复]
【发布时间】:2020-04-22 16:57:27
【问题描述】:

some_product += another_product

我刚开始学习 python,所以我对这门语言很陌生,我很难理解 += 的含义。

【问题讨论】:

标签: python syntax


【解决方案1】:

这是一个非常简单的例子,说明如何向变量添加数字:

a = 1
# we want to add to a 5
a = a + 5
print(a)
# output: 6

或:

a = 1
a += 5 # equivalent with a = a + 5
print(a)
# output: 6

类似的: some_product += another_product 等效于:some_product = some_product + another_product,假设您的变量是整数或其他数字类型

【讨论】:

  • 这是整数的一个很好的例子,但是我们不知道 OP 是否在谈论实现 __iadd__ / __add__ 的整数、字符串、列表或自定义类。
  • 是的,刚刚更新了我的答案
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 2011-01-09
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 2015-02-20
相关资源
最近更新 更多