我一直在关注Python3.11中关于Enum格式化的来回讨论,参见https://github.com/python/cpython/issues/100458.
然而,我有一些与实现__str__
的类的混合,并且在3.11的次要版本中看到了不一致的行为,特别是3.11.0-3.11.3.
使用以下脚本,在3.10年末到3.12年初的几个不同版本上运行:
import enum
class A:
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return f'{self.a}|{self.b}'
class Foo(A, enum.Enum):
BAR = 'a', 'b'
import sys
print(sys.version)
print(Foo.BAR)
print(str(Foo.BAR))
print("%s" % Foo.BAR)
print(f"{Foo.BAR}")
print("{}".format(Foo.BAR))
输出:
3.10.13 (main, Oct 25 2023, 19:27:56) [Clang 15.0.0 (clang-1500.0.40.1)]
a|b
a|b
a|b
a|b
a|b
3.11.0 (main, Oct 25 2023, 19:22:31) [Clang 15.0.0 (clang-1500.0.40.1)]
Foo.BAR
Foo.BAR
Foo.BAR
Foo.BAR
Foo.BAR
3.11.3 (main, Oct 25 2023, 12:37:31) [Clang 15.0.0 (clang-1500.0.40.1)]
Foo.BAR
Foo.BAR
Foo.BAR
Foo.BAR
Foo.BAR
3.11.4 (main, Oct 25 2023, 00:36:48) [Clang 15.0.0 (clang-1500.0.40.1)]
a|b
a|b
a|b
a|b
a|b
3.11.6 (main, Oct 25 2023, 12:41:05) [Clang 15.0.0 (clang-1500.0.40.1)]
a|b
a|b
a|b
a|b
a|b
3.12.0 (main, Oct 25 2023, 19:30:35) [Clang 15.0.0 (clang-1500.0.40.1)]
a|b
a|b
a|b
a|b
a|b
我只是想确认3.11.0-3.11.3版本是某种已经修复的回归,并且预期的行为是future 的3.11.4+行为.