Perspective array custom properties

I wish someone could help me understand what is happening in this simplified sample code here. I have an array, arr0, defined as a custom property (on view, root, etc. same result), read it to local arr1, and modify one of the elements of the local array, var0, but the results do not make any sense.
This only happens with array. I usually hesitate to jump and call something a 'bug', but it seems to be it.

arr1 = self.view.custom.arr0 is not making a copy of that arr0 array - it is essentially saying "For all intents and purposes, I am the same thing as arr0."

So when you modify the value of the internal object (var0), the in-memory value which changes is the value referened by both arr1 AND self.view.custom.arr0[1].var0.

It's a weird behavior of Python that can surprise people more use to Java or C. This site explains it a bit.

1 Like

Thanks for the prompt reply. Although unexpected, but I can understand the referenced effect and hence the last print result. Although, what I still can't comprehend is the second print, right after assigning 222 to arr1.var0, it still prints 111

Ah, I see what you're saying.
I'm just placing this here for reference as we look into this...

	self.parent.custom.array[1].var0 = 111
	array1 = self.parent.custom.array
	system.perspective.print(self.parent.custom.array[1].var0)
	array1[1].var0 = 222
	system.perspective.print(array1[1].var0)
	system.perspective.print(self.parent.custom.array[1].var0)
1 Like

Interesting. If I log the id for the two arrays it turns out we are actually doing a copy on the back-end in order to make this more like Java.

	self.parent.custom.array[1].var0 = 111
	array1 = self.parent.custom.array
	system.perspective.print(id(self.parent.custom.array)) 
	system.perspective.print(id(array1))
	system.perspective.print(self.parent.custom.array[1].var0)
	array1[1].var0 = 222
	system.perspective.print(array1[1].var0)
	system.perspective.print(self.parent.custom.array[1].var0)

Note: the 8 and 9 values are just because I ran this a few times before copying the logging.

09:58:54.623 [Browser Thread: 58950] INFO Perspective.Designer.Workspace - 8
09:58:54.623 [Browser Thread: 58950] INFO Perspective.Designer.Workspace - 9
09:58:54.624 [Browser Thread: 58950] INFO Perspective.Designer.Workspace - 111
09:58:54.624 [Browser Thread: 58950] INFO Perspective.Designer.Workspace - 111
09:58:54.625 [Browser Thread: 58950] INFO Perspective.Designer.Workspace - 222

If we are indeed making a copy, then neither of the last two lines makes sense. I'll reach out to a Developer to get some better insight into what might be happening here.

1 Like

Thanks again for the attention and reply.