473,785 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling destructor in python multiply-inherited class

Hi,
I have two classes, say, Base1 and Base2. Now Derv is a class derived
from both like
class Derv(Base1,Base 2).....
In the destructor of the derived class, if I write something like this

def __del__(self):
for b in self.__class__. __bases__:
b.__del__(self)

then its working fine, but if I am doing something straightforward
like

def __del__(self):
Base1.__del__(s elf)
Base2.__del__(s elf)

Its giving an error like :
Exception exceptions.Attr ibuteError: "'NoneType' object has no
attribute '__del__'" in <bound method Derv.__del__ of <__main__.Der v
instance at 0x8187484>> ignored

Obviously I'm doing something wrong ! Any pointers ??

Thanks in advance!
Jul 18 '05 #1
1 4815
ra******@fastma il.fm (Rajorshi) wrote in
news:85******** *************** ***@posting.goo gle.com:
In the destructor of the derived class, if I write something like this

def __del__(self):
for b in self.__class__. __bases__:
b.__del__(self)

then its working fine, but if I am doing something straightforward
like

def __del__(self):
Base1.__del__(s elf)
Base2.__del__(s elf)

Its giving an error like :
Exception exceptions.Attr ibuteError: "'NoneType' object has no
attribute '__del__'" in <bound method Derv.__del__ of <__main__.Der v
instance at 0x8187484>> ignored

Obviously I'm doing something wrong ! Any pointers ??


I guess this error occurs when your program is exiting. When a Python
program terminates the global variables in each module are set to None. The
order in which this happens it undefined, so when your __del__ method gets
called the global Base1, or maybe Base2, has already been cleared. The base
classes still exist, so you can access them through your class, but the
names they used will have been rebound.

A reasonable conclusion from this is:
A __del__ method must never access *any* global variables unless it is
prepared to handle the consequences of the global not existing.

Next question, do you really need to use __del__? Remember that Python will
not always call the __del__ method at the time you expect, and that the
behaviour will vary on different Python implementations . Also the existence
of a __del__ method is sufficient to prevent your object ever being
released from a cycle by the garbage collector. If you need explicit tidy
up, use a dispose pattern instead of depending on __del__.

There is very little that you can usefully do inside a __del__ method.
Deleting contained objects is pointless (it happens automatically without
you doing it in __del__). Closing a file should be done explicitly when you
are finished with it because if you try to do it in __del__ and the call to
__del__ is delayed you will get unexpected problems.

If you really need to call __del__ on more than one base class, use the
super() builtin to do it.
Jul 18 '05 #2

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

Similar topics

3
1998
by: François Miville-Dechêne | last post by:
I find your language very nice, it is actually more than three quarters of what I had been dreaming for years a programming language should be. But I mourn the passing of APL, another interpreted language. I like interpreted languages, for they can be used in calculator mode, testing function after function without bothering to code long protocols in the language and outside it. One suggestion I would propose would be to make common...
1
1922
by: engsolnom | last post by:
Knowing absolutely zero about benchmarks, I was interested in the posting referencing the paper by Dr. Cowell-Shah. I was curious how long Python takes just to read and display time.clock(), soI made the simple test below. I was amazed at the difference in speed depending on when/how the clock times were converted to milliseconds....almost 4 to 1...as indicated in the RESULTS below. I understand the variation in the times is probably due...
1
1577
by: tom fogal | last post by:
Hi all, I can't seem to find out how to get a python script to run from a C (well, C++...) program. In particular, I'm confused about how the execution of the example code at http://docs.python.org/ext/pure-embedding.html works. If I call the example code 'multiply.py' and I generate an executable named 'call' (as the page suggests), I had expected the following to work: ../call multiply.py multiply 3 2
16
9732
by: Roman Suzi | last post by:
Hi, I wonder, does Python support generic programming paradigm, and to what extent (I guess, it doesn't do it in full)? And (or) does it move in that direction? Will we ever see concept WellAlright: ... constructs in Python?
14
2422
by: Timothy Madden | last post by:
Hello I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list is arbitrary long. Something like this: class ListElem { char Datum;
50
2909
by: Gosi | last post by:
It is quite easy to call J from Python http://groups.google.com/group/J-Programming/browse_thread/thread/5e84b75667f5f64e
12
3141
by: Tom_chicollegeboy | last post by:
here is what I have to do: This question involves a game with teddy bears. The game starts when I give you some bears. You then start giving me back some bears, but you must follow these rules (where n is the number of bears that you have): If n is even, then you may give back exactly n/2 bears. (Hint: To test whether n is even, use the expression ((n % 2) == 0).) If n is divisible by 3 or 4, then you may multiply the last two digits
2
3751
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
"delete" does two things: 1) Invokes the destructor 2) Deallocates the memory We can manually invoke the destructor with: p->~T(); But is there any way to manually deallocate the memory without invoking
7
16952
by: Xstrain | last post by:
Having difficulty creating error handling when inputting an integer dvdQuant = input("Enter quantity of DVD: ") how can I get python to return a print "Error Message" if an integer is not entered instead of Enter quantity of DVD: gyfj Traceback (most recent call last): File "I:/Python/CourseworkVer3.py", line 203, in <module> addDvd(catalogue) File "I:/Python/CourseworkVer3.py", line 129, in addDvd
0
186
by: Marcin Krol | last post by:
Hello everyone, I'm trying to embed Python interpreter in C code, but in a specific way: loading compiled bytecode into a memory location and executing it (don't ask why, complicated reasons). PyImport_ExecCodeModule seems like obvious candidate, docs say: "Given a module name (possibly of the form package.module) and a code object read from a Python bytecode file or obtained from the built-in
0
9645
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
9481
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
10341
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...
1
10095
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
8979
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
7502
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
5383
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...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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

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.