To keep it simple, I wanna do a new type that emulate a coordinate in 2D space.
class Vector2:
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def __str__(self): # for print()
return f"({self.x}, {sefl.y})"
But I went into a problem, when I assign a vector to another variable, any change to one of them will affect both.
>>> A = Vector2(1.0, 2.0)
>>> B = A # my problem is that
>>> A.x = 2.0 # changing the x attribute of A
>>> print(B) # affect B and I don't want that
(2.0, 2.0)
I know some special methods (__set__
or __get__
) exist which can make that when writing this B = A
, B
will be a copy of A
instead of A
himself.
Now, what special method should I use and how should I use it?
I tried to find an anwser there but I didn't found.
specification: I'm using python3.9.5 on linux