【发布时间】:2021-09-29 21:28:28
【问题描述】:
为什么它会给出不同的输出:
+和+=有区别吗
def func1(lst1, lst2):
lst1 += lst2
lst1 = [1, 2, 3]
lst2 = ["a", "b"]
func1(lst1, lst2)
print(lst1)
和
def func1(lst1, lst2):
lst1 = lst1 + lst2
lst1 = [1, 2, 3]
lst2 = ["a", "b"]
func1(lst1, lst2)
print(lst1)
提前致谢
【问题讨论】:
-
第一个程序给出:[1,2,3] 第二个:[1,2,3,'a','b','c']
-
这能回答你的问题吗? What exactly does += do in python?
-
@NirAlfasi 在 3.8 中运行我也得到两个不同的输出。诚实的问题,如果这两个运算符相同并且只是语法糖(这是我的假设),为什么我们会从
print()的lst1得到两个不同的响应? -
这能回答你的问题吗? Why does += behave unexpectedly on lists?
-
@NirAlfasi 我删除了那部分,我的错。
标签: python