以上这种赋值,当a变量被改动时,b变量也会跟着改动。
列表:
字典:
A = B = {} # Both A and B refer to the same empty dictionary object
A['key'] = 'value'
print(B) # Output: {'key': 'value'}
集合:
A = B = set() # Both A and B refer to the same empty set object
A.add(1)
print(B) # Output: {1}
自定义对象:
class MyClass:
def __init__(self, value):
self.value = value
obj1 = MyClass(10)
obj2 = obj1 # Both obj1 and obj2 reference the same object
obj1.value = 20
print(obj2.value) # Output: 20
当连续赋值是bool类型是,不会一起改动。
例如:hasLower = hasUpper = hasDigit = hasSpecial = False