I do not understand what goes wrong with the following code:
class MyClass:
def __init__(self, val = [0, 1]):
self.val = val
def giveout(self):
print(self.val)
def swap(self):
inter = self.val[0]
self.val[0] = self.val[1]
self.val[1] = inter
a = MyClass()
a.swap()
a.giveout()
b = MyClass()
b.giveout()
Output:
[1, 0]
[1, 0]
Why is the output not:
[1, 0]
[0, 1]
When b gets initialised without a parameter, should it not get [0, 1]
as default parameter? When I replace swap()
by:
def swap(self):
self.val = [self.val[1], self.val[0]]
Then the output is as I would expect.