fork download
  1. class A:
  2. def __init__(self, x):
  3. self.x = x
  4. self.cleanup_done = False
  5.  
  6. def close(self):
  7. print(f'A x={self.x} being cleaned up.')
  8. self.cleanup_done = True
  9.  
  10.  
  11. def __del__(self):
  12. if not self.cleanup_done:
  13. self.close()
  14.  
  15.  
  16. class B:
  17. def __init__(self, x):
  18. self.a = A(x)
  19.  
  20. def foo(self):
  21. print("I am doing some work")
  22.  
  23.  
  24. def bar():
  25. b = B(9)
  26. b.foo()
  27.  
  28. def other_function():
  29. pass
  30.  
  31. if __name__ == '__main__':
  32. bar()
  33. other_function()
Success #stdin #stdout 0.02s 9176KB
stdin
Standard input is empty
stdout
I am doing some work
A x=9 being cleaned up.