欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python print不能立即打印的解决方式

时间:2021-08-21 09:10:32|栏目:Python代码|点击:

1、问题描述

在Python中使用print打印hello world时,终端不显示

def hello():
 print("hello world!")

2、原因

因为标准输入输出stdin/stdout有缓冲区,所以使用print不能立即打印出来,作为刚接触Python的菜鸟,迷瞪了半天

3、解决方法

1)刷新缓冲区,python中是sys.stdout.flush()

import sys 
def hello():
 print("hello world!")
 sys.stdout.flush()

2)python3中支持print支持参数flush

原型:

print(*objects, sep=' ‘, end='\n', file=sys.stdout, flush=False)

def hello():
 print("hello world!", flush=True)

参考官方手册

https://docs.python.org/zh-cn/3/library/functions.html#print

上一篇:Python中实现单例模式的n种方式和原理

栏    目:Python代码

下一篇:python3下载抖音视频的完整代码

本文标题:Python print不能立即打印的解决方式

本文地址:http://www.codeinn.net/misctech/168634.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有