ipython的atexit替代方案
python有一个模块来注册要在关闭解释器之前运行的函数。很好地解释了为什么不叫“脱欧” 我想知道ipython的atexit替代方案,ipython,python-3.5,ipython-magic,Ipython,Python 3.5,Ipython Magic,python有一个模块来注册要在关闭解释器之前运行的函数。很好地解释了为什么不叫“脱欧” 我想知道ipython是否有一种替代方法,可以在退出使用%run运行的程序之前注册一个函数?理想情况下,我想创建一个装饰器,它可以在任何一种情况下工作 模块取决于解释器。感谢Thomas K的好评。如果他写了答案,我会接受他的。否则,这段代码可能会使其他人受益: # exit_register runs at the end of ipython %run or the end of the python
ipython
是否有一种替代方法,可以在退出使用%run
运行的程序之前注册一个函数?理想情况下,我想创建一个装饰器,它可以在任何一种情况下工作
模块取决于解释器。感谢Thomas K的好评。如果他写了答案,我会接受他的。否则,这段代码可能会使其他人受益:
# exit_register runs at the end of ipython %run or the end of the python interpreter
try:
def exit_register(fun, *args, **kwargs):
""" Decorator that registers at post_execute. After its execution it
unregisters itself for subsequent runs. """
def callback():
fun()
ip.events.unregister('post_execute', callback)
ip.events.register('post_execute', callback)
ip = get_ipython()
except NameError:
from atexit import register as exit_register
@exit_register
def callback():
print('I\'m done!')
print('Running')
在
%run
之后没有特定的触发,但是您可以执行%run
加上一些操作,或者在执行每个输入之后运行。这非常有效。还有一件事要补充atexit.register
可以接受参数和关键字参数。考虑更改<代码> ExistyReals<代码>,允许通过:<代码> DEF EXITY寄存器(Fun,*ARGS,*kWARGS)< /C>和<代码> Fun(*ARGs,**KWARGS)< /C>。