0

Hello community,

        SourcePoints = self.SourcePoints
        TargetPoints = self.TargetPoints

        print("SourcePoints: ", self.SourcePoints)
        print("TargetPoints: ", self.TargetPoints)

        for point in SourcePoints:
            for i in range(len(point)):
                point[i] = Origin[i] + point[i] * Spacing[i]
        for point in TargetPoints:
            for i in range(len(point)):
                point[i] = Origin[i] + point[i] * Spacing[i]

        print("SourcePoints: ", self.SourcePoints)
        print("TargetPoints: ", self.TargetPoints)

What you probably need to know for context: - self.SourcePoints/self.TargetPoints is initialized once an instance of the class (that inherits the code) is created. There is no other section where the lists are manipulated. - self.SourcePoints/self.TargetPoints are lists of lists - the code snipped is part of a function within the class

I am by far not an expert in programming, so i might oversee something obvious here, but i cant see it and it drives me crazy, because the two print commands dont have the same output...and the script crashes (ofc) because self.SourcePoints/self.TargetPoints are "wrong" in the subsequent iteration.

So you might say now, by writing

        SourcePoints = self.SourcePoints

you dont make a copy, but create a reference to the list, so naturally the origin-list gets changed during the for-loops. Makes perfect sense to me, thats why i tried

        SourcePoints = copy.copy(self.SourcePoints)

and

        SourcePoints = self.SourcePoints.copy()

which both didnt change anything.

Then i said to myself, maybe its because of the identical name of the lists. So the script thinks if i take the variable "SourcePoints" within a class function i want to change the variable self.SourcePoints. So i changed everything to

        srcPoints = self.SourcePoints.copy()

And it still doesnt work... I have really no idea what is going on and it drives me crazy. Fortunately i have a bypass for the script, but i am still trying to figure out why this keeps happening. It makes absolutely no sense to my how the two print commands dont have identical output. Yet, the computer is just doing what i tell him to do.

Can somebody enlighten me?

Greets, Void

Voidling
  • 72
  • 7
  • I didn't quite get what is actually wrong. What were you expecting or trying to do? And what does it actually give you? – Klarth May 08 '20 at 13:33
  • @Klarth i am using the shown snipped to segment a volume automatically. i further use an iteration in which i change arbitrary parameters. self.SourcePoints/self.TargetPoints are initialized once an instance is created (beginning of iteration). The shown code is from a function within the class that gets called once in every iteration. problem is, that the shown for loops change the origin lists aswell (so the second iteration fails), although only the SourcePoints/TargetPoints are supposed to change as needed within the function. – Voidling May 08 '20 at 13:38
  • @Klarth the print functions before and after are supposed to output the same. I dont make that clear in my post, excuse me. And several techniques dont change it and i can't understand why. – Voidling May 08 '20 at 13:40

1 Answers1

0

You're creating shallow copies of the instance variable. To get a fully independent copy of an object you can use the copy.deepcopy() function.

Try making the below changes to your first 2 lines

import copy
SourcePoints = copy.deepcopy(self.SourcePoints)
TargetPoints = copy.deepcopy(self.TargetPoints)

You might find this post helpful for more information on shallow and deep copying - Understanding dict.copy() - shallow or deep?

bhavi
  • 170
  • 1
  • 6