I have problem with understanding what happens with the instance variable in the following case:
class Permutation:
def __init__(self,cycles):
self.cycles=cycles
def inverse(self):
c=self.cycles
for i in c:
r=math.ceil(len(i)/2)
for j in range(r):
i[j],i[-(j+1)]=i[-(j+1)],i[j]
return c
p = Permutation([[1,5,2],[3, 7]])
print(p.cycles)
p.inverse()
print(p.cycles)
output:
>>>
[[1, 5, 2], [3, 7]]
[[2, 5, 1], [7, 3]]
I have a class named Permutation with instance variable cycles. When I want to calculate the inverse of the permutation, I define new variable c, which should be the same as self.cycles and then I calculate the inverse of variable c. When I create an instance p=Permutation(...) and call p.inverse(), p.cycles actually changes and I'm not sure why.
Could someone please explain what is happening with variable cycles?
Thank you!