【发布时间】:2015-06-26 00:23:19
【问题描述】:
我想让一个子类'__str__ 实现添加到基本实现中:
class A:
def __str__(self):
return "this"
class B(A):
def __str__(self):
return super(B, self) + " + that"
然而,这会产生类型错误:
TypeError: +: 'super' 和 'str' 的操作数类型不受支持
有没有办法让str(B())返回"this + that"?
【问题讨论】:
-
这不是你的问题,但在 Python 3 中你可以只写
super(),不需要旧的 2.x 语法super(B, self)。
标签: python inheritance