← index #1878Issue #1802
Duplicate · high · value 2.400
QUERY · ISSUE

py: __del__ special method not implemented for user-defined classes

openby israelg99opened 2016-03-07updated 2024-12-05
py-core

Example:

import gc

class Foo():
    def __del__(self):
        print('__del__')

f = Foo()
del f

gc.collect()

According to this post #1802, gc.collect() should collect the object after the del and call the finaliser, but the finaliser is never called, any ideas?

I know that a good programming practice is to assume that __del__ may never be called, but hey, this still should work :)

CANDIDATE · ISSUE

del should call __del__ method

closedby blazewiczopened 2016-01-28updated 2016-03-07

Example test with custom object:

class Foo():
    def __del__(self):
        print('__del__')

f = Foo()
del f  # should print '__del__'

Also for builtin types with finaliser, like socket.socket.
Example case:

s = socket.socket()
s.connect(addr)  # binds "hard" socket to object `s`
...
del s  # s.close() has not been called - "hard" socket will never be released

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied