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?
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to