Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python class memory handling when inheriting an array from another class

How does python class handle an array when the array is inherited from another class? Does the child class store the array from a parent class as a pointer or does it perform a deep copy and use up extra memory?

For example, if I had a code like

class parentclass(object):
 def __init__(self):
   self.parentarray = some array

class childclass(object):
 def __init__(self,parentclass):
  self.childarray = parentclass.parentarray

parent_cls = parentclass()
childclass(parent_cls)

does the program hold two of the same arrays or just one array with one pointer?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks

>Solution :

The general rule in Python is that assignment almost never makes a copy. You just end up with two names to the same array object.

It’s easy to test, if you make a modification to the one array you’ll see the same change in the other.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading