python:如何覆盖str.join?
我们有一个 str 的子类(称之为 MyStr),我需要能够控制 str.join 如何与我的子类交互。
至少,所有 MyStr 的连接应该生成另一个 MyStr,并且 MyStr 和“普通”str 的连接应该抛出 TypeError。
目前,发生的情况如下:(MyStr 子类 unicode)
>>> m = MyStr(':')
>>> m.join( [MyStr('A'), MyStr('B')] )
u'A:B'
>>> ':'.join( [MyStr('A'), 'B', u'C'] )
u'A:B:C'
We have a subclass of str (call it MyStr), and I need to be able to control how str.join interacts with my subclass.
At minimum, a join of all MyStr's should produce another MyStr, and joining of MyStr and "plain" str should throw a TypeError.
Currently, this is what happens: (MyStr subclasses unicode)
>>> m = MyStr(':')
>>> m.join( [MyStr('A'), MyStr('B')] )
u'A:B'
>>> ':'.join( [MyStr('A'), 'B', u'C'] )
u'A:B:C'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
join()
是一个str
方法。如果您想最终得到一个MyStr
对象,则可以使用MyStr
对象来进行连接。如果你想要一个
TypeError
,你就必须不继承str
并自己提供str
的所有方法(至少是那些你需要)。不过,这很可能会使它们对于正常的字符串操作基本上毫无用处。join()
is astr
method. If you want to end up with aMyStr
object afterwards, you to use aMyStr
ojbect to do the join.If you want a
TypeError
you'll have to not inherit fromstr
and provide all ofstr
's methods yourself (at least the ones that you need). It's quite possible, though, that this will make them largely useless for normal string operations.你的类不能重写
join
吗:这至少会涵盖
MyStr(...).join(...)
的情况在@bukzor的评论之后,我查了一下它是如何工作的,看起来 join 是一个 C 函数,当使用
unicode
分隔符调用时,它总是返回一个unicode
对象。可以在此处查看代码。看一下
PyUnicode_Join
函数,尤其是这一行:因此,
PyUnicode_Join
的结果将始终是PyUnicode
的实例。我能看到的唯一错误情况是输入不是 unicode:
所以我认为这种情况不可能失败(至少,当您的对象从
unicode
扩展时不会失败):Couldn't your class just override
join
:This will at least cover the case of
MyStr(...).join(...)
After @bukzor's comment, I looked up how this works, and it looks like join is a C function that always returns a
unicode
object when called using aunicode
seperator.The code can be seen here. Take a look at the
PyUnicode_Join
function, especially this line:So, the result of
PyUnicode_Join
will always be an instance ofPyUnicode
.The only error case I can see is if the input isn't unicode:
So I don't think it's possible to make this case fail (at least, not while your object extends from
unicode
):