不要使用f=file("data.txt","wt"),而是使用更现代的with-statement语法(如上所示)。
▍__enter__ () 在进入with-statement主体之前调用 返回绑定到变量的值 可以返回任何类型的值 通常返回上下文管理器本身 ▍__exit__() 当语句体退出时调用 __exit
参考文献 contextlib — Utilities for with-statement contexts - Python 「Python进阶 #1」上下文管理器Context Manager -
内建对象 __main__ --- 顶层脚本环境 warnings --- Warning control dataclasses --- 数据类 contextlib --- Utilities for with-statement
fact * sign # The "+s" rounds back to the original precision, # so this must be outside the with-statement
fact * sign # The "+s" rounds back to the original precision, # so this must be outside the with-statement