📜  python 阻止打印输出 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:19.768000             🧑  作者: Mango

代码示例2
import io
import sys
def salute(name): 
  """Says hi to someone."""    
  print('Hi, {}!'.format(name))
# create a text trap and redirect 
stdouttext_trap = io.StringIO()
sys.stdout = text_trap
# execute our now mute function
salute('Anne')
# now restore 
stdout function
sys.stdout = sys.__stdout__