PVectors in Class Objects

When we have a variable assignment, for example this 1: targ_pos = PVector(100, 300, 0)

What is actually stored in the variable is the memory address (a.K.a. reference or pointer) of an object.

If we have more than 1 variable, property or container assigned to the same reference value from another, they all become alias to the same object.

In order to avoid aliased variables, make sure to clone an object before assigning it.

For PVector, its methods copy() or get() will create a clone of it:

For convenience, do it right there inside the constructor:

class Foo:
    def __init__(self, targ_pos, mis_pos):
        self.targ_pos = targ_pos.get()
        self.mis_pos = mis_pos.get()
        self.l_o_s = PVector()
2 Likes