内置常量

内置常量#

名称 NoneFalseTrue__debug__ 无法重新赋值(赋值给它们,即使是属性名,将引发 SyntaxError),所以它们可以被认为是“真正的”常量。

演示如下:

False = 'e'
True = 5
None  = 'w'
__debug__ = 2

NotImplemented#

NotImplementedtypes.NotImplementedType 类型的唯一实例。

type(NotImplemented)
NotImplementedType

作为布尔值来解读 NotImplemented 已被弃用。虽然它目前会被解读为真值,但将同时发出 : DeprecationWarning。它将在未来的 Python 版本中引发 TypeError

bool(NotImplemented)
C:\Users\xinzo\AppData\Local\Temp/ipykernel_4936/3586968809.py:1: DeprecationWarning: NotImplemented should not be used in a boolean context
  bool(NotImplemented)
True

当二元(或就地)运算返回 NotImplemented 时,解释器将尝试对另一种类型(或其他一些回滚操作,取决于运算符)实施反射操作。如果所有尝试都返回 NotImplemented,则解释器将引发适当的异常。错误返回的 NotImplemented 将导致误导性错误消息或返回到 Python 代码中的 NotImplemented 值。

from demo.a import A, B
a = A(4)
b = B(4)
a == b
Comparing an A with a B
True
b == a
Could not compare B with the other class
Comparing an A with a B
True