No, references are cheap as just a few data are transferred. Unfortunately you can get such side effects as described above. So you need to know about that.
copy() does what you expects - it copies the object (the vector). You get a reference to the copy (rather than the original).
If you modify the copy, the original will not be influenced.
The = operator means "assign what is on the right side to the left side.
This means if the right side gives you a reference, the reference will be assigned.
If the right side is a value (string, int, float…) the value will be assigned.
In that way the reference is a value as well ;).
How to know it is a reference? This is not easy. Thumb rule:
If it is a primitive type (string, number) it is usually a value.
If it is complex (list, dictionary, module, class, function) it is usually a reference.
If you really want to know use the “is” attribute or the “id()” function.
data = ownerObject.worldPosition
if data is ownerObject.worldPosition:
print( "data is a reference to ownerObject.worldPosition")
#if id(data) == id(ownerObject.worldPosition):
if data == ownerObject.worldPosition:
print("data contains data that are equal to ownerObject.worldPosition")
You will see the same thing on function arguments/parameters. This is especially dangerous as you might not see the side effect.
Therefore a good rule is:
- avoid changing an argument within a function
If you need to pass data back,do it by return or use a class
helloWorldString = append(helloString, " World")
def append(prefix, suffix):
return prefix + suffix
#rather than prefix += suffix
helloWorldContainer = StringContainer("Hello")
helloWorldString = helloWorldContainer.completeString
def __init__(self, prefix):
def append(self, suffix):
I hope it does not confuse you.
As agoose77 wrote: “The most important thing is readability.”
It does not matter if it works. (As a programmer you should be able to create code that works). If it is hard to read it is bad code. A professional programmer creates clean code (which indeed works and is readable ).