473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1680
On Sep 28, 6:00*pm, Marcin201 <marcin...@gmail.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**********************************@p25g2000hsf. googlegroups.com>,
Marcin201 <ma*******@gmail.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...@gmail.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
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...
9
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...
11
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"...
16
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...
6
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...
11
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
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...
35
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...
14
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
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) {...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.