load/store - create new object?
load/store - create new object?
In .attr function, should I create a copy of the object when storing/loading or use the one that was passed in?
Re: load/store - create new object?
You could do either. It really depends on your application and whether the object passed in is mutable or not. If it is mutable, do you want to see changes that are made?
If you were to do something like
x = [ 1, 2, 3]
YourFunc(x)
x[1] = 4
Do you want your stored value to be [1,2,3] or [1,4,3]?
The answer to that question is the answer to your question.
If you were to do something like
x = [ 1, 2, 3]
YourFunc(x)
x[1] = 4
Do you want your stored value to be [1,2,3] or [1,4,3]?
The answer to that question is the answer to your question.
Re: load/store - create new object?
Is there a way, in Python language, to distinguish between those two types of loads?dhylands wrote: ↑Fri Mar 02, 2018 11:35 pmYou could do either. It really depends on your application and whether the object passed in is mutable or not. If it is mutable, do you want to see changes that are made?
If you were to do something like
x = [ 1, 2, 3]
YourFunc(x)
x[1] = 4
Do you want your stored value to be [1,2,3] or [1,4,3]?
The answer to that question is the answer to your question.
In my application, I have a deeply nested hierarchy of struct-like data and I can definitely imagine a case where giving the user the option to distinguish between getting a reference and getting a copy of a deeply-nested data item so that they don't have to do `a.b.c.d.e.f.g`