在Python3中,我发现此意外的TypeError:
bpython version 0.17.1 on top of Python 3.6.9 /usr/bin/python3
import re
from collections import UserString
s = UserString('foo')
re_repetitions = re.compile(r"(/)\1{1,}", re.DOTALL)
re_repetitions.sub(r"\1", s)
Traceback (most recent call last):
File "<input>", line 1, in <module>
re_repetitions.sub(r"\1", s)
TypeError: expected string or bytes-like object
-为什么会这样?有标准的解决方法吗?
UserString
的实例不是str
的实例,但它contain