Object destruction delayed on interactive prompt

Mario Figueiredo marfig at gmail.com
Sun Jan 25 13:23:43 EST 2015


Consider the following class:
 class A:
 def __init__(self, value):
 self.value = value
 def __del__(self):
 print("'A' instance being collected...")
 def __repr__(self):
 return str(self.value)
If ran as a script, the destructor behavior works as expected:
 if __name__ == '__main__':
 x1 = A(12)
 print(x1) # __repr__()
 x1 = 2 # __del__()
 print(x1)
But if I run it interactively, a weird behavior comes up:
 >>> x1 = A(12)
 >>> x1 = 'string'
 A instance being collected... # __del__() as expected
 >>> x1 = A(12)				# Recreate x1
 >>> x1					# __repr__()
 12
 >>> x1 = 'string' # __del__() isn't executed!
 >>> x1 # it is delayed until here
 A instance being collected...
 'string'
This is reproducible in IDLE and at the system command prompt. Is this a 
REPL specific behavior?


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /