我注意到 SO 中与 Python 相关的相当多的问题是关于人们搞砸了 Sys 类、OS 类和没有类。例如,一个容易混淆的情况是:os.open("something")
、open("something")
和 sys.open("something")
。我还没有理解类命名背后的原因,也许这只是一种演变。
- 我想听听
为什么
他们是用现在的名字创建的? - 命名是否是由于类(class)中有 FD 之类的原因?
- 命名是因为某些类需要特殊权限吗?
- 命名在多大程度上是一种设计解决方案?
如果你不能回答这个问题,请随意提出一些好的助记法来内存类和区分它们。
最佳答案
Easy confusing is lies in the case: os.open("something"), open("something") and sys.open("something").
“助记符”是文档,可在线获取或下载到您的工作站。
内置
打开
:http://docs.python.org/library/functions.html#open “打开一个文件”os.open
: http://docs.python.org/library/os.html#os.open “此函数用于低级 I/O。”sys.open
不存在。io.open
: http://docs.python.org/library/io.html#io.open “在 Python 3.x 中,它是访问文件和流的默认接口(interface)”
“助记”很简单。使用符合您要求的那个。
why they were created with their current names
在单独的库中避免语言困惑。
Are naming due to things like having FDs in a class?
可能吧。 FD 是操作系统功能,而不是语言功能。这就是为什么它们位于单独的库中。
Is naming because some classes require special privileges?
完全没有。
To which extent is the naming a design solution?
在单独的库中避免语言困惑。
关于python - OS 和 SYS 等容易混淆的 Python 类命名背后的原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5602439/