Jun-12-2022, 06:50 PM
(Jun-11-2022, 10:07 PM)deanhystad Wrote: Python objects live only as long as they are referenced. Unreferenced objects get garbage collected and their memory is reused to make new objects. color = "blue" assigns a str object to a variable named "color". Right now there is only one reference to the str object "blue". If I do this, color = 1, the variable color now references an int object. Now the str object "blue" is not referenced by any variables. It's reference count is zero. There is no way for me to use the str object "blue" anymore because I don't have any variables that reference it. In many languages "blue" would continue to exist in limbo, taking up space until the program ends. In Python, assigning color = 1 not only assigns color to reference an int object, it unassigns color to stop referencing the "blue" object. The "blue" object reference count drops to zero, and Python puts it in the garbage collector.
In your example where you do this: df = df.append([['grey',12]]), the variable "df" is reassigned to reference the new dataframe object created by the append() function. The original dataframe object is no longer referenced by any variables, so it gets garbage collected.
Is that clear?
That's good stuff. So the answer to my question about where the new dataframe object goes if not assigned is that it disappears into the recycle bin. Thanks!