为什么 __rmod__ 不能正确处理字符串?
Why doesn't __rmod__ work properly for strings?
>>> class MyInt(int):
... def __rmod__(self, other):
... return 42
...
>>> class MyStr(str):
... def __rmod__(self, other):
... return 'wat'
...
>>> 0 % MyInt()
42
>>> '%r' % MyStr()
"''"
为什么int
subclass可以从反射端控制这个BinOp,而str
不能?这似乎与记录的 datamodel 相矛盾。
我希望使用该功能创建一个非侵入式且向后兼容的扩展,为 logging
框架提供大括号样式 handlers/formatters,但这让我停滞不前.那是一个错误吗?
Python Linux 3.6.0。使用 collections.UserString
作为基础 class 也有问题。使用 bytes
作为基础不会。
这是 Python issue 28598. The fast path for %
string formatting in the bytecode evaluation loop wasn't checking for string subclasses. It's fixed now,所以请将您的 Python 更新到 v3.6.1+。
>>> class MyInt(int):
... def __rmod__(self, other):
... return 42
...
>>> class MyStr(str):
... def __rmod__(self, other):
... return 'wat'
...
>>> 0 % MyInt()
42
>>> '%r' % MyStr()
"''"
为什么int
subclass可以从反射端控制这个BinOp,而str
不能?这似乎与记录的 datamodel 相矛盾。
我希望使用该功能创建一个非侵入式且向后兼容的扩展,为 logging
框架提供大括号样式 handlers/formatters,但这让我停滞不前.那是一个错误吗?
Python Linux 3.6.0。使用 collections.UserString
作为基础 class 也有问题。使用 bytes
作为基础不会。
这是 Python issue 28598. The fast path for %
string formatting in the bytecode evaluation loop wasn't checking for string subclasses. It's fixed now,所以请将您的 Python 更新到 v3.6.1+。