473,796 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__del__ not called?

Hi,

I do not understand why __del__ does not get executed in the following
example.

test.py:

#!/usr/bin/python
class A(object):
def __init__(self):
print "init"

def __del__(self):
print "del"

test1.py

#!/usr/bin/python
import test

class B(object):
a = test.A()

Running test1.py outputs:

init

the "del" is missing.

I suppose the a object should be garbage collected!?
--
Gregor
Mar 13 '06 #1
8 8379
Em Seg, 2006-03-13 Ã*s 08:21 +0100, Gregor Horvath escreveu:
Hi,

I do not understand why __del__ does not get executed in the following
example.


It only collects when there are no references:
class A(object): .... def __init__(self):
.... print "A's init"
.... def __del__(self):
.... print "A's del"
.... a = A() A's init del a A's del class B(object): .... a = A()
....
A's init del B
# We'll to tell him to collect the garbage here, but .... # usually it should not be necessary.
.... import gc gc.collect() A's del
20


--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação' . Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

Mar 13 '06 #2
Felipe Almeida Lessa schrieb:
del B
# We'll to tell him to collect the garbage here, but


... # usually it should not be necessary.


Thanks. If I do

del B

then the __del__ of A gets called.
That surprises me. I thought that B gets del'd by python when it goes
out of scope?

Do I manually have to del all class objects, so that their class
attributes get gc'd ????

--
Gregor
Mar 13 '06 #3
Gregor Horvath wrote:
#!/usr/bin/python
class A(object):
def __init__(self):
print "init"

def __del__(self):
print "del"

test1.py

#!/usr/bin/python
import test

class B(object):
a = test.A()

Running test1.py outputs:

init

the "del" is missing.

I suppose the a object should be garbage collected!?


No, Python doesn't run the garbage collector when it is exiting. What it
does is to delete all the globals from each module in turn. So:

C:\Python24>pyt hon
Python 2.4.2c1 (#66, Sep 21 2005, 15:16:11) [MSC v.1310 32 bit (Intel)] on
win32

Type "help", "copyright" , "credits" or "license" for more information.
class A(object): .... def __init__(self):
.... print "init"
.... def __del__(self):
.... print "del called"
.... a = A() init ^Z
del called

C:\Python24>

In this case the del method is called as 'a' is deleted, but if you create
a circular reference a does not get destroyed:
class A(object): .... def __init__(self):
.... print "init"
.... def __del__(self):
.... print "del called"
.... a = A() init a.ref = a
^Z

C:\Python24>

What is less obvious is that new style classes always include circular
references, so a class is never detroyed until the garbage collector runs.
A.__mro__ is a tuple which includes A, and there is probably something else
I've forgotten (for an empty and otherwise unreferenced class
getrefcount(old styleclass) returns 2, getrefcount(new styleclass) returns
5).

Of course, if your __del__ method actually does get invoked during program
exit you have to be pretty careful what you do: the chances are any global
variables you used in __del__ have already been destroyed in particular any
modules you imported may have been deleted. In short, don't rely on
anything much being possible from __del__ called this way.
Mar 13 '06 #4
Duncan Booth schrieb:
What is less obvious is that new style classes always include circular
references, so a class is never detroyed until the garbage collector runs.
Thanks. I tried the same example with old style classes and A.__del__
gets correctly called.
Of course, if your __del__ method actually does get invoked during program
exit you have to be pretty careful what you do: the chances are any global
variables you used in __del__ have already been destroyed in particular any
modules you imported may have been deleted. In short, don't rely on
anything much being possible from __del__ called this way.


I wanted to close a database connection, which is opend by __init__.

But what happens to my database connection (instance attributes of A)
when __del__ is never called?

--
Greg
Mar 13 '06 #5
[Duncan Booth]
No, Python doesn't run the garbage collector when it is exiting.
Actually, it does. What it doesn't do is call the garbage collector
twice when it exits, although it used to ;-)
What it does is to delete all the globals from each module in turn. So:


Yup. The code is in function Py_Finalize(). Here's the relevant snippet:

...
PyGC_Collect();

/* Destroy all modules */
PyImport_Cleanu p();

/* Collect final garbage. This disposes of cycles created by
* new-style class definitions, for example.
* XXX This is disabled because it caused too many problems. If
* XXX a __del__ or weakref callback triggers here, Python code has
* XXX a hard time running, because even the sys module has been
* XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
* XXX One symptom is a sequence of information-free messages
* XXX coming from threads (if a __del__ or callback is invoked,
* XXX other threads can execute too, and any exception they encounter
* XXX triggers a comedy of errors as subsystem after subsystem
* XXX fails to find what it *expects* to find in sys to help report
* XXX the exception and consequent unexpected failures). I've also
* XXX seen segfaults then, after adding print statements to the
* XXX Python code getting called.
*/
#if 0
PyGC_Collect();
#endif

The first PyGC_Collect() runs, then what you described runs ("destroy
all modules"), and then PyGC_Collect() _doesn't_ run again. As the
comment says, it's the second run of PyGC_Collect() that _would_ get
rid of dead module-level new-style classes, were it to run.

Alas, as the XXX comments say, too much of the interpreter has been
destroyed by PyImport_Cleanu p() for __del__ and weakref callbacks to
execute sanely, so we have to skip it. And, of course, module-level
objects _aren't_ trash before PyImport_Cleanu p() runs. Therefore
module-level objects involved in reference cycles never trigger
__del__ or weakref callbacks as a side effect of Python exiting, and
new-style classes are (as you said) always involved in reference
cycles.
Mar 13 '06 #6
Gregor Horvath wrote:
Of course, if your __del__ method actually does get invoked during
program exit you have to be pretty careful what you do: the chances
are any global variables you used in __del__ have already been
destroyed in particular any modules you imported may have been
deleted. In short, don't rely on anything much being possible from
__del__ called this way.


I wanted to close a database connection, which is opend by __init__.

But what happens to my database connection (instance attributes of A)
when __del__ is never called?


First off, never depend on __del__ to do anything critical. The only
guarantee about the __del__ method on an object is that it will be called
zero, one, or more times. (Admittedly you have to work a bit to get it
called more than once.)

If you have a resource which absolutely must be tidied up, then always put
the code which accesses that resource inside a try..finally construct. If
it is something pretty global to your program than that try..finally might
have to be at the outermost level of your program:

try:
main()
finally:
cleanup()

(In Python 2.5 you will be able to use a 'with' construct instead, but
unfortunately we still have to wait a bit for that to become common usage).

Without knowing more about the specific database connection, I can't tell
you what happens if you don't explicitly close it. I would hope that it
will tidy itself up, but if your code keeps anything cached locally to be
written out then obviously that might not get written.

If the database supports transactions (and it should), then I would expect
anything modified in a transaction which has been commited will be written
correctly, and everything modified in a transaction which has not been
commited will be discarded: closing (or not) the database should be pretty
well irrelevant.
Mar 13 '06 #7
Duncan Booth schrieb:
First off, never depend on __del__ to do anything critical. The only


Thanks to all of you!
Everything's clear now!

--
Greg
Mar 13 '06 #8
Gregor Horvath wrote:
Felipe Almeida Lessa schrieb:
>del B
># We'll to tell him to collect the garbage here, but
... # usually it should not be necessary.


Thanks. If I do

del B

then the __del__ of A gets called.
That surprises me.


Why ?
I thought that B gets del'd by python when it goes
out of scope?
It does. What you have to understand here is that in your script, B
being in the global (read : module) scope, it doesnt goes out of scope
before the script's execution's done. By that time, it's too late to do
anything with stdin/stdout/stderr.

Just add this to the script:

def foo():
b = B()
print "in foo"

foo()
Do I manually have to del all class objects, so that their class
attributes get gc'd ????


Absolutely not. The only times I use del is when I want to cleanup a
namespace from temp vars no longer used (usually a module's namespace
with some hairy tricks at load time).

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Mar 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2476
by: seth | last post by:
Last week I encountered an AttributeError in my unit tests that I wasn'table to catch with an "except AttributeError" statement. The problem stemmed from a class that raised an error inside __init__and defined a __del__ method to clean up resources. I then discovered asimilar problem in the shelve module. This led me to two importantdiscoveries: 1. Attributes defined in __init__ after an error is raised will not be apart of the...
2
1645
by: Kepes Krisztian | last post by:
Hi ! I very wonder, when I get exp. in java with GC. I'm Delphi programmer, so I get used to destructorin objects. In Java the final method is not same, but is like to destructor (I has been think...). And then I try with some examples, I see, that the Java GC is
6
2594
by: Peter Abel | last post by:
I have an application, which is an instance of a class with a deeply nested object hierarchy. Among others one method will be executed as a thread, which can be stopped. Everything works fine except that when deleting the main instance - after the thread has been stopped - the __del__ method will not be carried out. Tough a simple example works as expected: >>> class A: .... def __init__(self):
2
2086
by: Mike C. Fletcher | last post by:
I'm looking at rewriting parts of Twisted and TwistedSNMP to eliminate __del__ methods (and the memory leaks they create). Looking at the docs for 2.3's weakref.ref, there's no mention of whether the callbacks are held with a strong reference. My experiments suggest they are not... i.e. I'm trying to use this pattern: class Closer( object ): """Close the OIDStore (without a __del__)""" def __init__( self, btree ): """Initialise the...
2
1909
by: Alex VanderWoude | last post by:
I am attempting to save my window's size and position when it closes. So I figured I'd put some code in the __del__() method: from wxPython import * class MyWindow(wxFrame): def __init__(self, parent, id=wxID_ANY, title=None, style=None): # Some stuff here. def __del__(self): x, y = self.GetPositionTuple() width, height = self.GetSizeTuple()
2
3329
by: flupke | last post by:
Hi, i have a class and a class attribute log which is a logger object. In the __del__() function i want to log a message but it fails even if i use self.__class__.log. The error i get is this: Traceback (most recent call last): File "C:\Python24\lib\logging\__init__.py", line 712, in emit self.stream.write(fs % msg)
13
1411
by: Torsten Bronger | last post by:
Hallöchen! When my __del__ methods are called because the program is being terminated, I experience difficulties in calling functions that I need for a clean shutdown of my instances. So far, there has been only one of these functions, and a class-local alias solved the problem. However, now there are many of them. Is there a way to detect whether the program is being terminated? (In this case, I wouldn't care and return from...
10
3197
by: Max Yuzhakov | last post by:
Hello! It is correct behaviour for python to call __del__ on some identity of a class object more than once? In brief I shall describe a situation. Sorry for my english. For debugin purposes I'm put in my module global counters for counting __init__ and __del__ calls.
0
936
by: Holger Joukl | last post by:
Hi all, I've recently run into a problem that I haven't seen with python 1.5.2 and python 2.3. It seems that under certain conditions __del__ does not get immediately called when a local variable goes out of scope. I ended up with deadlocks in a threaded application because a locked section was supposed to be entered from within __del__ but apparently the same
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.