小编use*_*210的帖子

如何将__repr__与多个参数一起使用?

(Python 3.5.2)

__repr__为我的一个课程定义如下:

class d():
    def __init__(self):
        self._values = []
        return
    def __pos__(self):
        return self._values[0]
    def __repr__(self, value):
        self._values.append(value)
        return
Run Code Online (Sandbox Code Playgroud)

现在我想要

x = d()
print(x,"a")
print(+x)
Run Code Online (Sandbox Code Playgroud)

回来

a

但我得到了

TypeError: __repr__() missing 1 required positional argument: 'value'

我尝试了一些变化,比如print(x),"a"没有运气.

python python-3.x

0
推荐指数
2
解决办法
2450
查看次数

标签 统计

python ×1

python-3.x ×1