Difference between io.open vs open in python

狂风中的少年 提交于 2019-12-21 06:50:11

问题


In the past, there's codecs which got replaced by io. Although it seems like it's more advisable to use io.open, most introductory python classes still teaches open.

There's a question with Difference between open and codecs.open in Python but is open a mere duck-type of io.open?

If not, why is it better to use io.open? And why is it easier to teach with open?

In this post (http://code.activestate.com/lists/python-list/681909/), Steven DAprano says that the built in open is using the io.open in the backend. So should we all refactored our code to use open instead of io.open?

Other than backward compatibility for py2.x, are there any reason to use io.open instead of open in py3.0?


回答1:


Situation in Python3 according to the docs:

io.open(file, *[options]*)

This is an alias for the builtin open() function.

and

While the builtin open() and the associated io module are the recommended approach for working with encoded text files, this module [i.e. codecs] provides additional utility functions and classes that allow the use of a wider range of codecs when working with binary files

(bold and italics are my edits)



来源:https://stackoverflow.com/questions/33891373/difference-between-io-open-vs-open-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!