I fail to understand one of the result of this code – and especially what should I do as a "workaround":
#!/usr/bin/env python
a = [1, 2, 3]
b = a
print (a) # >> gives [1, 2, 3] # >> expected
print (b) # >> gives [1, 2, 3] # >> expected
b.clear()
print (a) # >> gives [] # >> ??
print (b) # >> gives [] # >> expected
Q1: Why is a
also cleared ?
Q2: What should I do if I want to keep a
as a sort of "template", so that, once set, do operations/modifications only on copies of it, without care about affecting the "original" in any way ?
(happens the same if a
is a dictionary; happens the same if using del b[:]
instead of b.clear()
)