I m trying to do a some activity on class obj destruction.
How do I achieve file open in __del__ function?
(I m using Python 3.4)
class iam(object):
def __init__(self):
print("I m born")
def __del__(self):
f = open("memory_report.txt", "w")
f.write("He gone safe")
f.close()
if __name__ == '__main__':
i = iam()
print("Script Ends. Now to GC clean memory")
Output:
I m born
Script Ends. Now to GC clean memory
Exception ignored in: <bound method iam.__del__ of <__main__.iam object at 0x00000000022F1A58>>
Traceback (most recent call last):
File "F:\Kumaresan\Code\Python\CommonLib\src\kmxPyQt\devConsole3\tet.py", line 14, in __del__
NameError: name 'open' is not defined
__del__at all is risky. Using it to do something like open a file is a sure road to trouble.__del__being called.__del__is called if I'm reading the docs correctly. As in OP's case where__del__is being called but can't findopen.