load/store - create new object?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

load/store - create new object?

Post by jickster » Fri Mar 02, 2018 9:48 pm

In .attr function, should I create a copy of the object when storing/loading or use the one that was passed in?

User avatar
dhylands
Posts: 3174
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: load/store - create new object?

Post by dhylands » 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.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: load/store - create new object?

Post by jickster » Sat Mar 03, 2018 11:14 pm

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`

Post Reply