1

I find object dispose pattern for python.
I found some pattern and write this test code;

class test:
     def __init__(self, *args, **kwargs):
         print("test.__init__(self, *args={}, **kwargs={})".format(args, kwargs))
     def __enter__(self, *args, **kwargs):
         print("test.__enter__(self, *args={}, **kwargs={})".format(args, kwargs))
     def __exit__(self, *args, **kwargs):
         print("test.__exit__(self, *args={}, **kwargs={})".format(args, kwargs))
     def __del__(self, *args, **kwargs):
         print("test.__del__(self, *args={}, **kwargs={})".format(args, kwargs))
     def example(self, *args, **kwargs):
         print("test.example(self, *args={}, **kwargs={}".format(args, kwargs))
         if("error" in kwargs):
             raise Exception(kwargs["error"])

I try this true call __del__ method.

t = test()
t.example(1, 2, 3, message="test")
t = None

result;

test.__init__(self, *args=(), **kwargs={})
test.example(self, *args=(1, 2, 3), **kwargs={'message': 'test'})
test.__del__(self, *args=(), **kwargs={}) ## Ok. I can see calling __del__ method

But this test after not call __del__

t = test()
t.example(1, 2, 3, error="some error")
t = None

result;

test.__init__(self, *args=(), **kwargs={})
t.example(1, 2, 3, error="some error")
test.example(self, *args=(1, 2, 3), **kwargs={'error': 'some error'}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 13, in example
Exception: some error
t = None
# Opss ! Not calling __del__ method

If some error raised after not calling __del___ Why ?

engin
  • 39
  • 5
  • https://stackoverflow.com/questions/1481488/what-is-the-del-method-how-to-call-it. __del__ only call when class finish its process while destructing object. whenever error occur its not finish running. – Avinash Dalvi Jun 17 '20 at 06:36
  • 2
    Yes. Exceptions are one of the reasons a `__del__` might not be called. This is even mentioned in [the documentation](https://docs.python.org/3/reference/datamodel.html#object.__del__) – juanpa.arrivillaga Jun 17 '20 at 06:40
  • 2
    "It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the cyclic garbage collector. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback." – juanpa.arrivillaga Jun 17 '20 at 06:44
  • Are you running this in a shell? @juan.arrivillaga I think the shell is holding the object in `sys.last_traceback`. I wrapped the call in a try/except block and `__del__` started working again. I then rerasied in the except block and `__del__` stopped again. Unfortunately, `sys.last_traceback = None` didn't fix it. However, `__del__` did work when code was run as a script. – tdelaney Jun 17 '20 at 07:03

0 Answers0