0

What's the suggested way to do just before the program ends in Python?

e.g. in c++, I can do something like

struct Cleanup { ~Cleanup() { ..do something..} };
....In some function... {
  static Cleanup cleanup; // this will be cleaned when program ends.
}

(I'm not sure whether the way above is a legitimate way in C++ but it seems working for me).

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Nan Hua
  • 3,414
  • 3
  • 17
  • 24

2 Answers2

2

Since you were specifically asking about the program finishing you may also want to look at the built-in atexit module.

https://docs.python.org/3/library/atexit.html

orangeInk
  • 1,360
  • 8
  • 16
0

Search about python destructor.

class Package:
    ...
    def __del__(self):
        pass

    def __exit__(self, exc_type, exc_value, traceback):
        pass

or decorator

@SomeDecorator
def function():
    pass

def SomeDecorator(func):
    def wrapper(self, *args, **kwargs):
        func(self, *args, **kwargs)
        # after func
return wrapper
Jiun Bae
  • 550
  • 3
  • 14