Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Dictionary เป็น Mutable จริงๆ หรือไม่

ใช่ Python Dictionary เป็น mutable การเปลี่ยนการอ้างอิงถึงคีย์ไม่ได้นำไปสู่การสร้าง dictionary ใหม่ แต่จะอัปเดต dictionary ปัจจุบันแบบ in-place

ตัวอย่าง

a = {'foo': 1, 'bar': 12}
b = a
b['foo'] = 20
print(a)
print(b)

ผลลัพธ์

{'foo': 20, 'bar': 12}
{'foo': 20, 'bar': 12}