[issue43310] Method __del__ with callable

Erik Soma report at bugs.python.org
Wed Feb 24 11:16:21 EST 2021


Erik Soma <stillusingirc at gmail.com> added the comment:

You can wrap your callable in a regular function:
```
def hack_c():
    c = C()
    def _(*args, **kwargs):
        return c(*args, **kwargs)
    return _
A.__del__ = hack_c()
```

Or (untested) make your callable an extension type with Py_TPFLAGS_METHOD_DESCRIPTOR.


Or instead of monkey-patching __del__ make __del__ call it:

```
class A:
   my_del = lambda *args, **kwargs: None
   def __del__(self):
       self.my_del(self)
A.my_del = C()
```



This doesn't just apply to __del__, other dunders exhibit this behavior as well. It is unintuitive, but I'm pretty sure it's not a bug.

----------
nosy: +esoma

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43310>
_______________________________________


More information about the Python-bugs-list mailing list