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:
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:
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?
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:
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:
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?