473,800 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

destructor not called

I have a class which uses a temporary directory for storing data. I
would like that directory to be removed when the class is no longer
used. I have tried removing the temporary directory from the class
destructor, however, it was never called. After I while I traced the
problem to the class having a reference to it's own function. Here is
a simplified model.

test.py
class Foo:
def __init__(self):
print "Hello"
self.f = self.fxn

def __del__(self):
print "Bye"

def fxn(self):
print "function"

a = Foo()

running python test.py I get
Hello

Is this an expected behavior or a bug in python? If this is expected
any suggestions for working around this. I would like to avoid having
to call the destructor explicitly.

Thanks,

Marcin
Sep 28 '08 #1
5 1702
On Sep 28, 6:00*pm, Marcin201 <marcin...@gmai l.comwrote:
I have a class which uses a temporary directory for storing data. *I
would like that directory to be removed when the class is no longer
used. *I have tried removing the temporary directory from the class
destructor, however, it was never called.
The RAII (Resource Acquisition Is Initialization) pattern is not
applicable to Python since the language concept is not suitable for
it. The __del__ is not a genuine destructor. In your case it might not
be performed when you expected it because there were still references
left around to the object. You must take care to break those
references.

However, you can apply the EAM (Execute Around Method) pattern in
Python to achieve the same effect. You can apply the EAM pattern with
help of the `with' statement:

with Foo() as a:
# work with `a'

In this case you must implement methods __enter__ and __exit__ instead
of __init__ and __del__. The method __enter__ must return an instance
of Foo.

You can achieve the same effect with try-finally block as well:

a = Foo()
try:
# work with `a'
finally:
# make `a' remove directories

Best Regards,
Szabolcs
Sep 28 '08 #2
In article
<2a************ *************** *******@p25g200 0hsf.googlegrou ps.com>,
Marcin201 <ma*******@gmai l.comwrote:
I have a class which uses a temporary directory for storing data. I
would like that directory to be removed when the class is no longer
used. I have tried removing the temporary directory from the class
destructor, however, it was never called.
The short answer is that destruction in Python is non-deterministic (a rude
shock if you're used to C++). What you probably want is the new "with"
statement (http://docs.python.org/ref/with.html).
Sep 28 '08 #3
Marcin201 a écrit :
class Foo:
def __init__(self):
print "Hello"
self.f = self.fxn
Maybe self.f = self.fxn() is what you want. Note the '()'.

--
Michel Leunen
http://linux.leunen.com
Sep 28 '08 #4
On Sep 28, 12:00*pm, Marcin201 <marcin...@gmai l.comwrote:
I have a class which uses a temporary directory for storing data. *I
would like that directory to be removed when the class is no longer
used. *I have tried removing the temporary directory from the class
destructor, however, it was never called. *After I while I traced the
problem to the class having a reference to it's own function. *Here is
a simplified model.

test.py
class Foo:
* * def __init__(self):
* * * * print "Hello"
* * * * self.f = self.fxn

* * def __del__(self):
* * * * print "Bye"

* * def fxn(self):
* * * * print "function"

a = Foo()

running python test.py I get
Hello

Is this an expected behavior or a bug in python? *If this is expected
any suggestions for working around this. *I would like to avoid having
to call the destructor explicitly.
Others have already replied to your main question; in short you
shouldn't rely on __del__ being called. Regardless, is there a (good)
reason for having an instance reference to the method ? Without
further information, that seems like a code smell.

George
Sep 28 '08 #5
Others have already replied to your main question; in short you
shouldn't rely on __del__ being called. Regardless, is there a (good)
reason for having an instance reference to the method ? Without
further information, that seems like a code smell.
I have dictionary of fxns to do import/export based on the type of
request from user so I can call self.Import['html'] or
self.Import['text'].

Thanks for everyones help.

Marcin
Sep 29 '08 #6

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

Similar topics

52
27043
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".
9
8277
by: sahukar praveen | last post by:
Hello, This is the program that I am trying. The program executes but does not give me a desired output. ********************************************** #include <iostream.h> #include <iomanip.h> #include <string.h>
11
10526
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor" is called in Case 2 since only ~base() becomes "virtual" and ~Derived() is still non-virtual? 3. Does Case 3 show that we don't need any virtual destructor to make ~Derived() called? 4. Is "virtual destructor" needed only for Case 2?
16
1866
by: Timothy Madden | last post by:
Hy I have destructors that do some functional work in the program flow. The problem is destructors should only be used for clean-up, because exceptions might rise at any time, and destructors will be called for clean-up only. So how can I tell, from within the destructor, if the call has been made as part of normal flow of control and the destructor can play its functional role, or if the call has been made as a result of an...
6
7967
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a particular struct (this struct is dynamically allocated by another provided function). To avoid memory
11
5313
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
4
3810
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For example ... C# Essentials (O'Reilly) - "Finalizers are class-only methods". Not sure what "class-only method" means in this context, but ususally I would assume that to mean it is static and would only be called once for the life of the...
35
3330
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a member. However, I set a breakpoint at the destructor of this instance's class and it was never called!!! I can see how it might not get called at a deterministic time. But NEVER? So, I guess I need to know the rules about destructors. I would...
14
3217
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
5
5527
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) { cout << "A::operator delete" << endl;
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10504
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
10274
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
10251
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
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.