Assignment of objects in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Assignment of objects in Python

Objects - on the heap Operation required-assignment (=) Assumptions - 1 objects will be moved from heap to stack for the assignment 2 obj1 has 6 properties obj2 has 7 properties Expected -MEMORY LEAK Is this correct ?

29th Oct 2020, 3:03 PM
Sanjay Kamath
Sanjay Kamath - avatar
4 Answers
2nd Nov 2020, 2:46 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Volodymyr Chelnokov does the assignment occur on the stack.. If so how does the garbage collector work? AFAIK this is on the heap... 🤔
1st Nov 2020, 3:43 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
If you think in terms of C, you can treat any Python variable as a pointer to PyObject. So object assignment is a pointer assignment. For getting rid of memory leaks Python has a garbage collector - any object that is not reachable (i. e. your program has no way of reading or modifying it in the future) is destroyed.
1st Nov 2020, 9:08 AM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 1
Sanjay Kamath all variables live on heap. Python stack is not the same thing as machine call stack - it is just a linked list of PyFrameObject structures, which also live on the heap. You can watch https://m.youtube.com/watch?feature=youtu.be&v=smiL_aV1SOc for information on it The only things that live on machine stack are internal variables of the interpreter.
1st Nov 2020, 4:14 PM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar