相关疑难解决方法(0)

Python继承:与super __str__连接

我想将一个子类的__str__实现添加到基本实现中:

class A:
    def __str__(self):
        return "this"

class B(A):
    def __str__(self):
        return super(B, self) + " + that"
Run Code Online (Sandbox Code Playgroud)

但是,这会产生类型错误:

TypeError:+:'super'和'str'的不支持的操作数类型

有办法获得str(B())回报"this + that"吗?

python inheritance

6
推荐指数
1
解决办法
8046
查看次数

标签 统计

inheritance ×1

python ×1