When I run the following program:
import threading
class foo(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def __enter__(self):
print "Enter"
def __exit__(self, type, value, traceback):
print "Exit"
def run():
print "run"
if __name__ == "__main__":
with foo() as f:
f.start()
I get this as the output
C:\>python test2.py
Enter
Exit
Traceback (most recent call last):
File "test2.py", line 17, in <module>
f.start()
AttributeError: 'NoneType' object has no attribute 'start'
Is there any way to combine the with keyword's guaranteed cleanup code execution with a threaded class?