Python 中 += 是什么意思?

发布于 2024-12-09 10:24:16 字数 107 浏览 0 评论 0原文

当它是这样的时候,它意味着什么:

self.something += ('somethin',)

“+=”是什么意思,逗号是什么意思?

What does it mean when it's like this:

self.something += ('somethin',)

What does "+=" mean and what does the comma mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷迭香的记忆 2024-12-16 10:24:16

表达式 a += ba = a + b 的简写形式,其中 ab 可以是数字、或字符串、或元组、或列表(但两者必须是同一类型)。

('x',) 中的逗号表示这是单个元素 'x' 的元组。如果缺少逗号,则只是括号之间的 'x'

The expression a += b is shorthand for a = a + b, where a and b can be numbers, or strings, or tuples, or lists (but both must be of the same type).

The comma in ('x',) means that this is a tuple of a single element, 'x' . If the comma is absent, is just an 'x' between parenthesis.

春风十里 2024-12-16 10:24:16

+= 是加法和赋值合一 (有时称为 iadd 或就地添加)。与a = a + x相同,

a = 4
a += 5  # add 5 to a, and assign the result into a
b = 4
b = b + 5   # this does the same thing as +=
print a  # prints out 9
print b  # prints out 9

您还可以用这种风格进行其他操作,例如-=*=、< code>/=、&=(按位与)、|=(按位或)、^=(按位异或) ), %= (mod), **=(指数)。


('something',) 是一个 元组< /a>. ('something') (不带逗号)在分组中使用括号,有点像 ('some' + 'thing')(a + b)。为了在语法上区分单成员元组和分组,Python 使用逗号。

+= is addition and assignment into one (sometimes referred to as iadd or in-place add). It is the same as a = a + x

a = 4
a += 5  # add 5 to a, and assign the result into a
b = 4
b = b + 5   # this does the same thing as +=
print a  # prints out 9
print b  # prints out 9

You can also do other operations in this style, such as -=, *=, /=, &= (bitwise and), |= (bitwise or), ^= (bitwise xor), %= (mod), **= (exponent).


('something',) is a tuple. ('something') (without the comma) is using the parenthesis in grouping, kind of like ('some' + 'thing') or (a + b). In order to differentiate between the one-member tuple and the grouping syntactically, Python uses a comma.

旧时浪漫 2024-12-16 10:24:16

Python 有一个为名称赋值的运算符,它是=

该语言还支持许多其他运算符,例如 +-**,用于在对象的特殊方法中定义的操作。

尽管 + 是添加内容的数学符号,但它可以自定义为执行您想要的任何操作。

有时您想要进行操作并使用相同的名称存储它。对于这些情况,您可以使用就地运算符,这些运算符只是用于加 = 符号的普通运算符。

对于不可变对象(数字、字符串、元组……),您不能进行就地更改,因为……它们是不可变的。因此,就地方法与普通方法执行完全相同的操作,然后进行赋值。

对于可变对象,区别非常明显:

就地添加:

>>> a = []
>>> b = a
>>> b += [1,2]
>>> a
[1, 2]

添加和分配:

>>> a = []
>>> b = a
>>> b = b + [1,2]
>>> a
[]

看到了吗?对象本身通过列表的就地添加进行了转换,但在另一种情况下,创建了一个新对象。


对于您的另一个问题,逗号是元组分隔符。

a = (1)   # Just number 1 inside parenthesis
a = (1,)  # A tuple with one element

Python has an operator to assign value to a name and it's =.

The language also support many other operators like +, -, ** for operations defined in special methods of your objects.

Although + is the math sign to add things, it can be customized to do whatever you want.

Sometimes you want to make an operation and store it using the same name. For these situations you have in-place operators that are just the normal operators you're use to plus the = sign.

For immutable objects (numbers, strings, tuples,...) you can't have in-place changes because... they're immutable. So, the in-place methods do exactly the same thing the normal method followed by an assignment.

For mutable objects the difference is much clear:

In-place add:

>>> a = []
>>> b = a
>>> b += [1,2]
>>> a
[1, 2]

Add and assign:

>>> a = []
>>> b = a
>>> b = b + [1,2]
>>> a
[]

See? The object itself was transformed with the in-place add for lists, but, on the other case, a new object was created.


For your other question, the comma is the tuple separator.

a = (1)   # Just number 1 inside parenthesis
a = (1,)  # A tuple with one element
你没皮卡萌 2024-12-16 10:24:16
results=[]
for x in range(5):
    results += '

此代码的行为与典型的 += 运算符不同,正如您在此处看到的,它生成了其中包含 $ 符号的列表。

它不像我们想象的那样 results = results+ '$' 此代码会抛出错误。

实际发生的情况是 += 运算符 的作用类似于列表中的 .extend()

print(results) output : ['

此代码的行为与典型的 += 运算符不同,正如您在此处看到的,它生成了其中包含 $ 符号的列表。

它不像我们想象的那样 results = results+ '$' 此代码会抛出错误。

实际发生的情况是 += 运算符 的作用类似于列表中的 .extend()

, '

此代码的行为与典型的 += 运算符不同,正如您在此处看到的,它生成了其中包含 $ 符号的列表。

它不像我们想象的那样 results = results+ '$' 此代码会抛出错误。

实际发生的情况是 += 运算符 的作用类似于列表中的 .extend()

, '

此代码的行为与典型的 += 运算符不同,正如您在此处看到的,它生成了其中包含 $ 符号的列表。

它不像我们想象的那样 results = results+ '$' 此代码会抛出错误。

实际发生的情况是 += 运算符 的作用类似于列表中的 .extend()

, '

此代码的行为与典型的 += 运算符不同,正如您在此处看到的,它生成了其中包含 $ 符号的列表。

它不像我们想象的那样 results = results+ '$' 此代码会抛出错误。

实际发生的情况是 += 运算符 的作用类似于列表中的 .extend()

, '

此代码的行为与典型的 += 运算符不同,正如您在此处看到的,它生成了其中包含 $ 符号的列表。

它不像我们想象的那样 results = results+ '$' 此代码会抛出错误。

实际发生的情况是 += 运算符 的作用类似于列表中的 .extend()

]

此代码的行为与典型的 += 运算符不同,正如您在此处看到的,它生成了其中包含 $ 符号的列表。

它不像我们想象的那样 results = results+ '$' 此代码会抛出错误。

实际发生的情况是 += 运算符 的作用类似于列表中的 .extend()

results=[]
for x in range(5):
    results += '

this code behaves differently from typical += operator , as you can see here it has generated list with $ sign inside it.

its is not like we think results = results+ '$' this code will throw you an error.

What actually happens is += operator works like .extend() in lists.

print(results) output : ['

this code behaves differently from typical += operator , as you can see here it has generated list with $ sign inside it.

its is not like we think results = results+ '$' this code will throw you an error.

What actually happens is += operator works like .extend() in lists.

, '

this code behaves differently from typical += operator , as you can see here it has generated list with $ sign inside it.

its is not like we think results = results+ '$' this code will throw you an error.

What actually happens is += operator works like .extend() in lists.

, '

this code behaves differently from typical += operator , as you can see here it has generated list with $ sign inside it.

its is not like we think results = results+ '$' this code will throw you an error.

What actually happens is += operator works like .extend() in lists.

, '

this code behaves differently from typical += operator , as you can see here it has generated list with $ sign inside it.

its is not like we think results = results+ '$' this code will throw you an error.

What actually happens is += operator works like .extend() in lists.

, '

this code behaves differently from typical += operator , as you can see here it has generated list with $ sign inside it.

its is not like we think results = results+ '$' this code will throw you an error.

What actually happens is += operator works like .extend() in lists.

]

this code behaves differently from typical += operator , as you can see here it has generated list with $ sign inside it.

its is not like we think results = results+ '$' this code will throw you an error.

What actually happens is += operator works like .extend() in lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文