- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
自从我知道了这个模式,我就一直在使用
with open('myfile.txt','w') as myfile:
with contextlib.redirect_stdout(myfile):
# stuff
print(...) # gets redirected to file
这让我可以使用打印语法(我更喜欢)写入文件,我可以轻松地将其注释掉以打印到屏幕以进行调试。但是,通过这样做,我将失去写入文件和屏幕的能力,并且可能编写不太清晰的代码。还有其他我应该了解的缺点吗?这是我应该使用的模式吗?
最佳答案
is this a pattern I should be using?
在这种特殊情况下,我确实认为您的模式不是惯用的,并且可能会让代码的读者感到困惑。内置的 print
(因为这是一个 Python-3x 问题)已经有一个 file
keyword argument这将完全按照 redirect_stdout
在您的示例中执行的操作:
with open('myfile.txt', 'w') as myfile:
print('foo', file=myfile)
并引入 redirect_stdout
只会让您的读者想知道您为什么不使用内置功能。 (就我个人而言,我发现嵌套的 with
很难看。\
分隔的 with
更难看。)
至于注释掉(以及打印到 stdout
和文件)的方便性,您可以根据需要进行任意多次 print
调用,并进行注释根据需要将它们取出
with open('myfile.txt', 'w') as myfile:
print('foo')
print('foo', file=myfile)
Are there any other disadvantages I should know about
除了它可能不是最佳解决方案(如本例)之外,我想不出任何确定的东西。
编辑:
来自doc :
Note that the global side effect on sys.stdout means that this context manager is not suitable for use in library code and most threaded applications. It also has no effect on the output of subprocesses.
关于python - contextlib.redirect_stdout 总是一个好主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35370052/
我正在更新一个旧的经典 ASP 站点并且我需要存储密码,因此考虑到经典 ASP 的局限性,我对如何处理这个问题自然有点过于谨慎。 我同时使用盐和胡椒(胡椒是存储在服务器上而不是数据库中的常量),我只是
我已经设置了我的数据库来记录每次失败的登录尝试。我以为我会将失败尝试的次数乘以 0.05 秒之类的。就像是: time_nanosleep(0, (50000000 * $fa
我想测量 1GB LAN 上消息代理的广播消息延迟。 消息以 pub sub 方式传输,一个发布者,多个消费者。生产者使用系统时钟(C# 中的 DateTime.Now)为每条消息加上时间戳,消费者通
我正在尝试学习代码契约并了解它们的用处。 我有一个 WPF 应用程序,因此很多代码注定要在 UI 线程上独占运行。相当多的实用程序类期望仅从 UI 线程调用。 在我的代码中加入这些是个好主意吗?为什么
我加入了一家小公司,该公司主要销售一种网络应用程序。 Web 应用程序中的数据非常敏感,以至于所有数据都经过字段级加密。应用程序使用 ASP.NET Web API 2(C#) 编写,前端为 html
对于我网站的后端,只有少数人可见,我有一个系统,通过 ajax 与 php 进行通信,如下所示: function ajax(url, opts) { var progress = false
我们有相当大的文档集,我们希望我们的用户能够查看和过滤这些文档。为了加快速度,我们只向客户发送有限数量的文件。但是,由于我们确实需要客户端能够过滤文档,因此我们需要一种方法来发送他们可以过滤的键和值。
我从 14 岁起就开始阅读游戏引擎书籍(那时候我什么都不懂:P)几年后,我想开始为我的游戏引擎编写数学基础。我一直在思考如何设计这个“图书馆”。 (我的意思是“有组织的文件集”)每隔几年就会出现新的
我们正在使用 Rails 为餐厅构建 SaaS 后端。我们直接与 POS 集成,因此每个 POS 不断发送我们存储的客户订单以供以后处理。我们在大约 1,000 个地点进行了这种 POS 集成,每月向
我正在设计一个系统,但我认为让最终用户能够删除数据库中的条目并不是一个好主意。我是这么认为的,因为通常最终用户一旦获得管理员权限,最终可能会在数据库中弄得一团糟,然后求助于我来修复它。 当然,如果他们
我正在考虑使用 RESTful Web 服务构建应用程序。我的想法是将应用程序的 RESTful(json 等)部分构建为独立的,然后将前端(例如 html/css/js/等)构建为该服务的客户端,虽
我有一种情况,我要保留对需要持久化的 ivar 的引用。在一个对象中,我有一个指向另一个对象中的 ivars 的指针数组,这些指针在程序的整个生命周期中都被使用。换句话说,我不只是传递一个引用来检索一
有没有prototype没有任何 DOM/ajax 部件的 fork?我真的更喜欢 jQuery,并且不想背负额外的包袱。到目前为止,我一直在使用优秀的 JS.Class库,它甚至不触及内置类原型(p
我正在从事的项目需要我编写大量重复代码。例如,如果我想在我的代码中加载一个名为“logo.png”的图像文件,我会这样写:位图标志图片; ... // Init logoImage = load("l
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 3年前关闭。 Improve t
我有一个小型网站,大约有 5-10 名管理员。我已将其设置为监视每个管理员正在做什么(添加项目、删除项目等)。我在我们的管理面板中有一个列表,显示了集体管理部门之前执行的 10 项事件。今天,我决定每
我看到将 PostgeSQL 数据库转储到一个大 SQL 文件中,然后提交并推送到远程 Git 存储库的方式可能是一个了不起的备份解决方案:我获得了所有版本的历史记录、散列、安全传输、单向(真的很难通
我正在开发一个需要本地化和国际化的 Web 应用程序。我突然想到我可以使用依赖注入(inject)框架来做到这一点。假设我声明了一个接口(interface) ILocalResources(在本示例
更新 :正如我所料,社区针对这个问题给出的合理建议是“衡量并观察”。 chibacity posted an answer一些非常好的测试为我做了这件事;同时,我自己写了一个测试;我看到的性能差异实际
我想知道使用 new 运算符创建对象但不将返回的对象分配给任何变量是否是个好主意。本质上,我只是在同一行中调用这些方法。例如: new Object().ToString(); 好吧,我知道上面的行不
我是一名优秀的程序员,十分优秀!