title: atexit alternative for ipython
body: <p>python has <a href="https://docs.python.org/3.5/library/atexit.html" rel="nofollow noreferrer">atexit</a> module to register functions to run prior to closing of the interpreter. <a href="https://stackoverflow.com/questions/21618769/atexit-function-is-not-called-when-exiting-the-script-using-ipython">This question</a> nicely why atexit is not called.</p>
  
  <p>I'm wondering if there is an alternative for <code>ipython</code> to register a function prior to exiting something which has been run with <code>%run &lt;name&gt;</code>? Ideally I would like to create a decorator which works registers in either 
  module depending on the interpreter.</p>