I am having a bit of trouble with a python class I have created. I have tried to create the most simple class possible to debug it by stripping out any unrelated code. I still have the same issue.
class MyClass(object):
def __init__(self):
self.row = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
def get_row(self):
return self.row
if __name__ == "__main__":
a = MyClass()
b = a.row
b.remove('1')
print(a.row)
print(b)
The output is:
['2', '3', '4', '5', '6', '7', '8', '9']
['2', '3', '4', '5', '6', '7', '8', '9']
What is happening is when I do the b.remove('1') it removes the first entry from the b list as expected but when I look at a.row it has also been removed from it. My understanding of classes and return values it that b should have been a copy of a, it appears to behave more like a pointer in this case.
Any help in explaining how this has actually worked would be much appreciated.


a.rowis the reference to the list, you are merely making a copy of that reference, not the value itself.