一次由默认参数引起的思考

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"题图来自 Pexels 上的 "},{"type":"link","attrs":{"href":"https://www.pexels.com/zh-cn/@shvetsa?utmcontent=attributionCopyText&utmmedium=referral&utm_source=pexels","title":""},"content":[{"type":"text","text":"Anna Shvets"}]},{"type":"text","text":" 拍摄的图片"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这是我对本人在语雀上书写的原始文档("},{"type":"link","attrs":{"href":"https://www.yuque.com/lart/blog/qhllk3","title":""},"content":[{"type":"text","text":"https://www.yuque.com/lart/blog/qhllk3"}]},{"type":"text","text":")的搬运。"}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"缘起"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近换了新机器。需要将旧设备上的代码和数据迁移过来。关键的一部分是自己论文使用的代码。为了确保代码迁移的完整性,在新机器上对其进行了验证。但是出乎意料,这部分python代码竟然输出了不一样的结果。经过多番排查,终于确定了问题所在,那就是其中使用的一个图像处理库——pillow,新机器上安装的是版本7.2.0,但是旧机器上确实6.2.1。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在pillow的release note("},{"type":"link","attrs":{"href":"https://pillow.readthedocs.io/en/stable/releasenotes/7.0.0.html#default-resampling-filter","title":""},"content":[{"type":"text","text":"https://pillow.readthedocs.io/en/stable/releasenotes/7.0.0.html#default-resampling-filter"}]},{"type":"text","text":")中可以看到,在7.0.0版本中,这个库进行了不兼容的修改。将用来放缩图像尺寸的 "},{"type":"codeinline","content":[{"type":"text","text":"resize()"}]},{"type":"text","text":" 函数默认的插值策略改成了双三次插值:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"The default resampling filter has been changed to the high-quality convolution "},{"type":"codeinline","content":[{"type":"text","text":"Image.BICUBIC"}]},{"type":"text","text":"  instead of "},{"type":"codeinline","content":[{"type":"text","text":"Image.NEAREST"}]},{"type":"text","text":" , for the "},{"type":"codeinline","content":[{"type":"text","text":"resize()"}]},{"type":"text","text":"  method and the "},{"type":"codeinline","content":[{"type":"text","text":"pad()"}]},{"type":"text","text":" , "},{"type":"codeinline","content":[{"type":"text","text":"scale()"}]},{"type":"text","text":"  and "},{"type":"codeinline","content":[{"type":"text","text":"fit()"}]},{"type":"text","text":"  functions."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而我原始的代码因为没有考虑这个问题,直接使用了默认参数,所以在新的环境下也就出现了这样“让人出乎意料”的问题。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这样的一次经历,让我对于代码中的默认参数有了一些新的看法。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一点想法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先就需要问自己一个问题,我们为什么需要“默认参数”?我觉得,归根到底,还是为了方便。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一切的优化、预设,在我看来,都是为了一个目的,“节省时间、珍爱生命”。为了尽可能避免开发者使用这些功能时需要考虑太多,尽可能的“开箱即用”,以将思考的时间用到更加重要的地方去。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是,这样就将我们对代码可以如期的、稳健的运行的“信任”和“期望”托付给了我们使用的第三方工具的开发者。虽然大多数情况下,我们可以对他们保持足够的信任,但是我们不得不考虑,第三方工具开发者对于所处领域的认识、对于相关知识的掌握程度、对于用户的可能行为的推测,都可能与我们这些使用者的实际情况有所偏差。所以我们真的很难、也不能完全,将我们的代码这辆“货车”的“未来”依附到一个具有着不可控因素的“发动机”上。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以我们应该怎么做呢?难道真的要放弃默认参数带来的便利么?虽然放弃就在很大程度上可以避免这样的问题(解决问题的一种直接的方法就是直接省掉出现问题的步骤 :
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章