[issue43310] Method __del__ with callable

2021-02-24 Thread Mark Dickinson


Mark Dickinson  added the comment:

Postscript: the "MethodType" that appears in the previous comment can be 
imported from the types module: "from types import MethodType".

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43310] Method __del__ with callable

2021-02-24 Thread Mark Dickinson


Mark Dickinson  added the comment:

The key difference between the func_del function and the instance C() is that 
func_del is a (non-data) descriptor in addition to being callable, while the 
instance C() is not. That makes func_del usable as a method.

If you define C as follows, you'll see the behaviour you expect:


class C:
def __call__(self, *args, **kwargs):
print("call", args, kwargs)

def __get__(self, obj, objtype=None):
  return MethodType(self, obj)

Useful reference for descriptors: 
https://docs.python.org/3/howto/descriptor.html

I'll close here, since this is neither a bug nor a feature request.

--
nosy: +mark.dickinson
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43310] Method __del__ with callable

2021-02-24 Thread Erik Soma


Erik Soma  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43310] Method __del__ with callable

2021-02-23 Thread Andrey Petukhov


New submission from Andrey Petukhov :

Is it possible to use callable to replace __del__ method of class?

if so, how to get self variable?

class A(object):
"""A."""


class C(object):
"""Callable."""
def __call__(self, *args, **kwargs):
print("call", args, kwargs)


def func_del(*args, **kwargs):
"""Method."""
print("func", args, kwargs)


def del_function():
"""
Test del.

('func', (<__main__.A object at 0x7f8ae5a82750>,), {})
('call', (), {})
"""
c = C()
A.__del__ = func_del
a = A()
del a
A.__del__ = c
a = A()
del a

--
components: Interpreter Core
messages: 387609
nosy: andribas404
priority: normal
severity: normal
status: open
title: Method __del__ with callable
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com