[Tutor] Python __del__ method

Jia Yue Kee JiaYue.Kee at firstsolar.com
Wed Jul 12 11:12:47 EDT 2017


Hi Eryk,

Thanks for the detailed explanation given. I think I got it now. :)

Regards,
JY

-----Original Message-----
From: eryk sun [mailto:eryksun at gmail.com] 
Sent: Wednesday, July 12, 2017 8:13 AM
To: tutor at python.org
Cc: Jia Yue Kee <JiaYue.Kee at firstsolar.com>
Subject: Re: [Tutor] Python __del__ method

On Wed, Jul 12, 2017 at 12:07 AM, eryk sun <eryksun at gmail.com> wrote:
> On Tue, Jul 11, 2017 at 2:47 PM, Jia Yue Kee <JiaYue.Kee at firstsolar.com> wrote:
>>
>> Case 2: If I were to run the code in "Interactive Mode", the following output will be obtained:
>>
>>>>> x = Robot("Tik-Tok")
>> Tik-Tok has been created!
>>>>> y = Robot("Jenkins")
>> Jenkins has been created!
>>>>> z = x
>>>>> z
>> <__main__.Robot object at 0x02D7E910>
>>>>> x
>> <__main__.Robot object at 0x02D7E910>
>>>>> del x
>>>>> del z
>>>>> del y
>> Robot has been destroyed
>>
>> My question being why is that "Robot has been destroyed" is only 
>> printed once in Case 2 (interactive mode) while it is printed out twice in Case 1 (script mode)?
>
> The REPL (interactive mode) keeps a reference to the last non-None 
> result as builtins `_`, for convenient access to the last result. In 
> your case, it results from evaluating `x`. Thus you have a hidden 
> reference that's keeping that object alive. Enter anything except None 
> or _ to clear that reference.

Well, not just any statement. It has to evaluate to another object except None or the Robot object that _ currently references. For example, enter 42.


More information about the Tutor mailing list