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

Home Posts Topics Members FAQ

Class destructor -- strange behaviour

Hi,

I have this simple code:
| #!/usr/bin/python
| import codecs
| import re
| from copy import deepcopy
|
| class MyClass(object) :
| def __del__(self):
| deepcopy(1)
|
| x=MyClass()

but I get an error:
| Exception exceptions.Type Error: "'NoneType' object is not callable"
in <bound method MyClass.__del__ of <__main__.MyCla ss object at
0x6fcf0>ignored

The problem disappears if I do anything of this:
1. change
- from copy import deepcopy
+ import copy
and call directly copy.deepcopy(1 )

or
2. don't store object to variable `x'

or
3. don't import module `re'

The first solution is OK, but I would like to know why it behaves so
strange. We have tested on:

- Mac OS X Tiger for PPC
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin

- Linux 64bit and 32bit
Python 2.4.4 (#1, Oct 30 2007, 14:31:50)
[GCC 4.1.2 (Gentoo 4.1.2 p1.0.2)] on linux2
Python 2.5 (r25:51908, Jan 12 2007, 13:57:15)
[GCC 4.0.2 20051125 (Red Hat 4.0.2-8)] on linux2

Thanks for the explanation,
Vlasta
Dec 6 '07 #1
2 2132
On Dec 6, 3:51 pm, Spes <Vlastimil.Ho.. .@gmail.comwrot e:
Hi,

I have this simple code:
| #!/usr/bin/python
| import codecs
| import re
| from copy import deepcopy
|
| class MyClass(object) :
| def __del__(self):
| deepcopy(1)
|
| x=MyClass()

but I get an error:
| Exception exceptions.Type Error: "'NoneType' object is not callable"
in <bound method MyClass.__del__ of <__main__.MyCla ss object at
0x6fcf0>ignored

The problem disappears if I do anything of this:
1. change
- from copy import deepcopy
+ import copy
and call directly copy.deepcopy(1 )

or
2. don't store object to variable `x'

or
3. don't import module `re'

The first solution is OK, but I would like to know why it behaves so
strange. We have tested on:

- Mac OS X Tiger for PPC
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin

- Linux 64bit and 32bit
Python 2.4.4 (#1, Oct 30 2007, 14:31:50)
[GCC 4.1.2 (Gentoo 4.1.2 p1.0.2)] on linux2
Python 2.5 (r25:51908, Jan 12 2007, 13:57:15)
[GCC 4.0.2 20051125 (Red Hat 4.0.2-8)] on linux2

Thanks for the explanation,
Vlasta
I can't explain why, but it also works if you call del x manually
before the script exits.

Regards,
Jordan
Dec 7 '07 #2
On Dec 6, 2:51 pm, Spes <Vlastimil.Ho.. .@gmail.comwrot e:
Hi,

I have this simple code:
| #!/usr/bin/python
| import codecs
| import re
| from copy import deepcopy
|
| class MyClass(object) :
| def __del__(self):
| deepcopy(1)
|
| x=MyClass()

but I get an error:
| Exception exceptions.Type Error: "'NoneType' object is not callable"
in <bound method MyClass.__del__ of <__main__.MyCla ss object at
0x6fcf0>ignored

The problem disappears if I do anything of this:
1. change
- from copy import deepcopy
+ import copy
and call directly copy.deepcopy(1 )

or
2. don't store object to variable `x'

or
3. don't import module `re'

The first solution is OK, but I would like to know why it behaves so
strange. We have tested on:

- Mac OS X Tiger for PPC
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin

- Linux 64bit and 32bit
Python 2.4.4 (#1, Oct 30 2007, 14:31:50)
[GCC 4.1.2 (Gentoo 4.1.2 p1.0.2)] on linux2
Python 2.5 (r25:51908, Jan 12 2007, 13:57:15)
[GCC 4.0.2 20051125 (Red Hat 4.0.2-8)] on linux2

Thanks for the explanation,
Vlasta
Gabriel nailed the problem down. Another solution is to explicitly
import module names in your destructor, ie:

def __del__(self):
from copy import deepcopy
deepcopy(1)

This guarantees that your destructor has "deepcopy" binding that you
want, even if the name has already been reassigned to None in your
current module by the interpreter as it is shutting down.

--Jason
Dec 7 '07 #3

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

Similar topics

28
4111
by: Steven T. Hatton | last post by:
This may be another question having an obvious answer, but I'm not seeing it. I'm trying to create a class that derives from std::valarray<std::string>. I don't need a template, and I haven't come across any examples of a construct like what I show below. Perhapes it's simply a bad idea. If there is something fundamentally wrong with this approach please let me know. Can anybody tell me if there is a way to get the following to work? I...
2
1714
by: Erik Friis | last post by:
Hi, I'm trying to create a class template like the following: template <class Type> class MyVector { MyVector(); ~MyVector(); unsigned int MyCount; Type* MyData;
7
1970
by: Rohit | last post by:
Iam writing an application that uses an abstract base class and a derived class that is implementation of the abstract base class. Say I have this piece of code: Derived *ptrToDerived=NULL; Base *ptrToBase=NULL; ptrToDerived= new Derived; ptrToBase=ptrToDerived; ..................... .....................
11
3089
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some methods. I don't show the Integer class because it will not add any information to my problem. Main is using some STL function Now to my problem.
3
1671
by: pareshgoel | last post by:
class B { virtual ~B(); } class D:public B { virtual ~D(); }
16
1993
by: Ajay | last post by:
Hi all, i want to know when i create a class.what all it contains.I know the following things are there by default if i do not declare them by myself.Please tell the other things that are left. 1. constructor 2.Destructor 3.Copy constructor 4. Assignment operator
3
3860
by: Juha Nieminen | last post by:
I once made my own smart pointer implementation for a project and at one point fought to death to find a malicious bug. The program was not working and I couldn't figure out why. The source of the problem was that MSVC++ was not giving me a warning even though it should have. The problem was that I was creating a smart pointer from a forward-declaration of a class which, it seems, makes it impossible for the smart pointer to call the...
7
4294
by: Rahul | last post by:
Hi Everyone, I was trying to implement a final class and i start having the destructor of the final class as private, class A { ~A() { printf("destructor invoked\n");
16
3450
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName), _netguid(netguid) {}
0
9483
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,...
1
10096
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
9956
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...
1
7504
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
5386
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
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
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.