Is it possible to merge two sets such that all references to both sets will refer to the new?
They currently all are referencing to their respective sets A
or B
, according to the assignments you've made. For instance in the case of bRef1
:
id(B)
# 112140097128
id(bRef1)
# 112140097128
The fact that the original set
is modified or not by modifying the new sets depends on the kind of operations you perform. If you perform an in-place operation, for instance:
bRef1 |= {5}
And check the original variable B
, you can see that indeed it has been updated:
print(B)
# {2, 4, 5}
Though when you merge both sets, unless you also perform an in-place operation and hence explicitly update one of the two sets, you will be creating a new object:
new_set = aRef2 | bRef2
id(new_set)
# 112140098248
But still, in the case you update in-place one of the sets A
or B
by merging it with the other, given that you've started out with 2
different objects, there is no way of changing that into all sets referencing the same object.