python-sys.stdout作为默认函数参数的实现
作者:codeday 时间:2022-11-23 02:50:35
假设我们有以下虚拟函数:
import sys
def writeline(text, stream=sys.stdout):
stream.write(text + '
')
with open('/path/to/file', 'w') as f:
# writes to /path/to/file
writeline('foo', f)
# writes to standard output
writeline('bar')
鉴于Python在定义时评估了函数的默认参数,将sys.stdout设置为默认参数是安全的,还是会产生意想不到的副作用?
最佳答案
我想到的一个问题是,您有时有时想自己将sys.stdout重定向到文件(或管道,设备等).
例如,您的主程序可能如下所示:
if __name__ == '__main__':
if len(sys.argv) > 1:
sys.stdout = open(sys.argv[1],'w')
try:
# ... run the program
finally:
if len(sys.argv) > 1:
sys.stdout.close()
如果您希望程序在提及一个文件时将其记录到一个文件中(例如python3 file.py logfile.log),这可能会很有用.现在,由于您设置了sys.stdout,因此您的writeline方法将不会注意到该修改.
因此,我认为写这样更安全:
def writeline(text, stream = None):
if stream is None:
stream = sys.stdout
stream.write(text + '
')
通常,最好将不可变对象设置为默认参数(例如None,False,(1)等).只有在极少数情况下,Python中才会使用不可变的变量(或可能更改引用的变量).
但是,如果您确定不会将sys.stdout重定向到文件,管道等,则是安全的.
补充拓展:Python 实现将sys.stdout还原为默认值
我想将输出写入文件,因此我做了
sys.stdout = open(outfile, 'w+')
但是后来我想在写入文件后打印回控制台
sys.stdout.close()
sys.stdout = None
我得到了
AttributeError: 'NoneType' object has no attribute 'write'
显然默认输出流不能None,所以我对Python说:
sys.stdout = use_the_default_one()
解决方案
重新分配给sys.__stdout__。
来自文档
在程序开始时包含stdin,stderr和stdout的原始值。它们在完成过程中使用,并且无论是否已重定向sys.std *对象,将其打印到实际的标准流都很有用。
或者你可以做
print(output, file=myoutputfile)
并避免该问题。
来源:https://codeday.me
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3.8安装tensorflow的简单方法步骤
![](https://img.aspxhome.com/file/2023/3/81663_0s.png)
一个ASP.NET的MYSQL的数据库操作类自己封装的
ElasticSearch核心概念
![](https://img.aspxhome.com/file/2023/4/114694_0s.png)
Python学习笔记(一)(基础入门之环境搭建)
![](https://img.aspxhome.com/file/2023/1/112731_0s.jpg)
ASP连接MySQL数据库代码示例2
python 自动批量打开网页的示例
Oracle将查询的结果放入一张自定义表中并再查询数据
python游戏实战项目之智能五子棋简易版
![](https://img.aspxhome.com/file/2023/7/134797_0s.gif)
Python api构建tensorrt加速模型的步骤详解
MySQL窗口函数的具体使用
![](https://img.aspxhome.com/file/2023/5/68675_0s.jpg)
Python+matplotlib实现华丽的文本框演示代码
![](https://img.aspxhome.com/file/2023/1/92531_0s.png)
Javascript对象中关于setTimeout和setInterval的this介绍
python画一个玫瑰和一个爱心
解决tensorflow模型压缩的问题_踩坑无数,总算搞定
MySQL中因一个双引号错位引发的血案详析
![](https://img.aspxhome.com/file/2023/7/128427_0s.jpg)
Active Server Pages 错误 ASP 0201 修复方法
详解使用CUDA+OpenCV加速yolo v4性能
![](https://img.aspxhome.com/file/2023/8/109338_0s.png)
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
![](https://img.aspxhome.com/file/2023/2/97492_0s.png)