为什么有些Python异常是小写?

huangapple go评论89阅读模式
英文:

Why are some Python exceptions lower-case?

问题

在Python中,异常是类并且被命名为类的方式。例如:OSError

然而,有一些异常,比如在socket模块中的异常,它们的命名是小写的。例如:socket.timeoutsocket.error

为什么会这样呢?

英文:

In Python, exceptions are classes and cased as such. For example: OSError.

However, there are some exceptions, such as those in the socket module, that are named in lower-case. For example: socket.timeout, socket.error.

Why is this?

答案1

得分: 7

根据文档,

异常 socket.error 是 OSError 的不推荐别名。

从版本 3.3 开始:根据 PEP 3151,此类别名已变为 OSError。

PEP 3151 中提到

虽然标准异常类型位于根命名空间中,但它们的可视区别在于它们使用 CamelCase 命名约定,而几乎所有其他内置类型都使用小写命名(除了 True、False、None、Ellipsis 和 NotImplemented)。

英文:

According to the docs,

> exception socket.error A deprecated alias of OSError.
>
> Changed in version 3.3: Following PEP 3151, this class was made an
> alias of OSError.

PEP 3151 says

> while standard exception types live in the root namespace, they are
> visually distinguished by the fact that they use the CamelCase
> convention, while almost all other builtins use lowercase naming
> (except True, False, None, Ellipsis and NotImplemented)

huangapple
  • 本文由 发表于 2020年1月7日 02:31:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定