Can anyone explain the 6th and 7th lines? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
18th Mar 2018, 12:19 AM
Vučko/Вучко/Vuchko
Vučko/Вучко/Vuchko - avatar
7 Answers
+ 13
Because they all make sense and I can not understand these two.
18th Mar 2018, 12:29 AM
Vučko/Вучко/Vuchko
Vučko/Вучко/Vuchko - avatar
+ 5
Python counts how often a data item is referenced so it knows the memory can be reused. By assigning a two times, it has a reference count of 3. By updating the variables in 6 and 7 to new values, they no longer reference the 42 so the count is decremented. Together with the delete of a, the reference count becomes 0 and the memory is available for reuse.
18th Mar 2018, 1:37 AM
John Wells
John Wells - avatar
+ 5
Putting on my moderator hat, since you don't know this, you must not have coded this. You are required to credit the original author or web site for any code you make public on this site. This is true no matter how little of the original is left. Please either give proper credit or make this code private. Thank you!
18th Mar 2018, 1:44 AM
John Wells
John Wells - avatar
+ 4
No, that just removes one reference so the count is 2 as both b and c still use it.
18th Mar 2018, 2:21 AM
John Wells
John Wells - avatar
+ 4
Here is an example using classes. Note there is only one constructor and destructor printout and the destructor is called on delete of b. https://code.sololearn.com/czi6GltHa04I
18th Mar 2018, 2:30 AM
John Wells
John Wells - avatar
+ 3
Got it John! Thank you very much. :)
18th Mar 2018, 2:32 AM
cyk
cyk - avatar
+ 2
I have a follow up question for John about this then. The count does not go to 0 the moment we do del a? After such a statement does it still matter that at some point we had b = a?
18th Mar 2018, 2:08 AM
cyk
cyk - avatar