如何在IronPython中覆盖打印

时间:2022-07-23 00:04:54

Is it possible to rebind print function for IronPython? I rebinded __builtins__["print"] to my custom function but it is not called when print is executed.

是否可以重新绑定IronPython的打印功能?我将__builtins __ [“print”]重新绑定到我的自定义函数,但是在执行打印时不会调用它。

1 个解决方案

#1


1  

That will only work in Python 2 if your program has

如果你的程序有,那只能在Python 2中使用

from __future__ import print_function

at the top. And (in Python 2 or Python 3) you don't have to do

在顶部。而且(在Python 2或Python 3中)您不必这样做

__builtins__["print"] = my_print_func

That is unnecessarily drastic and may have unforeseen side-effects in code you did not write. It is enough to change the definition of print in the local namespace, for example

这是不必要的激烈,并且可能在你没写的代码中产生无法预料的副作用。例如,在本地命名空间中更改print的定义就足够了

print = my_print_func

#1


1  

That will only work in Python 2 if your program has

如果你的程序有,那只能在Python 2中使用

from __future__ import print_function

at the top. And (in Python 2 or Python 3) you don't have to do

在顶部。而且(在Python 2或Python 3中)您不必这样做

__builtins__["print"] = my_print_func

That is unnecessarily drastic and may have unforeseen side-effects in code you did not write. It is enough to change the definition of print in the local namespace, for example

这是不必要的激烈,并且可能在你没写的代码中产生无法预料的副作用。例如,在本地命名空间中更改print的定义就足够了

print = my_print_func