Page 1 of 1

load/store - create new object?

Posted: Fri Mar 02, 2018 9:48 pm
by jickster
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?

Posted: Fri Mar 02, 2018 11:35 pm
by dhylands
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.

Re: load/store - create new object?

Posted: Sat Mar 03, 2018 11:14 pm
by jickster
dhylands wrote:
Fri Mar 02, 2018 11:35 pm
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.
Is there a way, in Python language, to distinguish between those two types of loads?
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`