0

I created a class that has attributes as shown in the code below then I build an electron object, in order to not redefine the whole object I made the positron = electron than I changed its name attribute the problem is that the two objects appeared to be pointing into each other "like C++" and any change in one object's attribute follow the same at the second's one, so how can I make a independent copy of an object into a different memory space independently.

The code:

class particule():
    def __init__(self,name,Mass,TotaleEnergy=None):
        super().__init__()
        self.name=name
        self.mass=Mass
        self.energy=(TotaleEnergy if TotaleEnergy!=None else self.mass)


electron=particule("e-",0.511)
positron=electron ; electron.name="e+"
print(positron.name , electron.name)#I get e+,e+
martineau
  • 119,623
  • 25
  • 170
  • 301
  • When you do `positron = electron`, you are making both variables "point" to the same object. As Leo Arad said, you'll need to copy one and store it on the other to avoid that problem. – Kexus Apr 13 '20 at 18:04
  • In Python names and objects aren't equivalent. This is explained in the [documentation](https://docs.python.org/3/reference/datamodel.html). This means that the `positron=electron` just gives the `particule` class instance another name. If you want a copy, you have to create one explicitly. – martineau Apr 13 '20 at 18:16
  • thanks, that was helpful guys – Slimane Mzerguat Apr 13 '20 at 18:24

3 Answers3

0

You can use the deepcopy from the copy module that will create you a new object identical to electron with a different id to be stored in positron.

from copy import deepcopy
positron=deepcopy(electron)

Because when you are doing the positron=electron it's creating an identical copy of the object electron and it's pointing to the same place in memory as the other object so if you change an attribute in one it will reflect the other.

Leo Arad
  • 4,452
  • 2
  • 6
  • 17
0

Python is a call by reference language, so whenever you do positron = electron it basically creates a new pointer that refers to the same element. So if you make changes to either of the objects, they will be reflected in both of them. What you need to do, in this case you are using a class, so if positron is going to inherit properties of electron you can make a child class or whatever as it suits the hierarchy.

0

What you did is called a shallow copy and is simply how Python works. Here is a link with documentation/explanation. Below are the definitions from that website.

A shallow copy means constructing a new collection object and then populating it with references to the child objects found in the original. In essence, a shallow copy is only one level deep. The copying process does not recurse and therefore won’t create copies of the child objects themselves.

A deep copy makes the copying process recursive. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. Copying an object this way walks the whole object tree to create a fully independent clone of the original object and all of its children.


My brief tl;dr explanation.

The variable named electron is pointing to a specific memory location, let's call it abcd1234.

When you set positron=electron you are simply pointing the positron variable name to the same memory location as electron(abcd1234)


How to Fix This

Either instantiate a seperate object.

Or

Avoid unintended shallow copies by using the copy library.

import copy

electron=particule("e-",0.511)
positron=copy.deepcopy(electron)
electron.name="e+"

print(positron.name , electron.name) #I get e-,e+

Here is another link for reference

Hope this helped!

Community
  • 1
  • 1
Darien Schettler
  • 546
  • 4
  • 13