If I write a module called testclass containing this:

import time
class TestClass(object):
    def __init__(self):
        self._createtime = time.strftime('%H:%M:%S', time.localtime()) 
        print "In __init__ at {} 'time' is bound to 
{}".format(self._createtime, time)
    def __del__(self):
        print "in __del__ for TestClass instance created at {} time is 
bound to {}".format(self._createtime, time)

And create another file, call it testmain.py, that consists of this:

import testclass

tc = testclass.TestClass()

The first time I run testmain.py from the Spyder console I get the 
intuitive result:
>>> runfile('/Users/evan/Desktop/testmain.py', wdir=r'/Users/evan/Desktop')
In __init__ at 22:09:14 'time' is bound to <module 'time' from 
'/Users/evan/anaconda/python.app/Contents/lib/python2.7/lib-dynload/time.so'>

But the second time I get weird behavior because the original module object 
has been killed by the UMD, so (for example) the original import of the 
time module is gone:
>>> runfile('/Users/evan/Desktop/testmain.py', wdir=r'/Users/evan/Desktop')
UMD has deleted: testclass
In __init__ at 22:09:15 'time' is bound to <module 'time' from 
'/Users/evan/anaconda/python.app/Contents/lib/python2.7/lib-dynload/time.so'>
in __del__ for TestClass instance created at 22:09:14 time is bound to None
If I were doing real work in __del__ then this would pose a problem

I've come up with three solutions:

   1. Don't use the UMD with classes that have __del__ methods. This works 
   fine, but I lose out on the UMD
   2. Clean up after myself: call "del tc" at the end of testmain.py. This 
   doesn't work so well. Not only am I stuck managing my own memory, but while 
   I'm doing development is totally possible that my code will hit an error 
   before I hit the "del tc" line. I can stick "del tc" in a finally block, 
   but then I'm *really* doing manual memory management.
   3. Have the __del__ method actually check whether its context has 
   already been cleaned up ("if time is None: ...") and if so vary its 
   behavior appropriately. Not suitable for all applications.

Is there a better workaround out there?

-- 
You received this message because you are subscribed to the Google Groups 
"spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.

Reply via email to