[英]How to remove [] on __repr__ return
When using repr to test the class that I have created, I always get brackets around my printed value. 当使用repr测试我创建的类时,我总是在我的打印值两边加上括号。
ex) EX)
def __repr__(self):
return ("H")
runs in shell as [H, H, H, H] for 4 lines. 在[H,H,H,H]中运行4行。 I want to remove the brackets and the commas but am unsure how to do this using repr . 我想删除方括号和逗号,但是不确定如何使用repr做到这一点。 Do i need to create another method that repr returns instead? 我是否需要创建另一个代表repr返回的方法?
Thank you for you help. 谢谢你的帮助。
Your example is not very clear, but I think the following code might demonstrate what you want: 您的示例不是很清楚,但是我认为以下代码可以演示您想要的内容:
>>> class Example:
def __repr__(self):
return 'H'
>>> array = [Example() for _ in range(4)]
>>> array
[H, H, H, H]
>>> class ArrayWithoutBrackets(list):
def __repr__(self):
return super().__repr__()[1:-1]
>>> array = ArrayWithoutBrackets()
>>> for _ in range(4):
array.append(Example())
>>> array
H, H, H, H
you must hide some thing. 你必须藏一些东西。 i think you have a list to contain all your instances, that's why you see a pair of [
, ]
. 我认为您有一个包含所有实例的列表,这就是为什么您看到一对[
, ]
。
i don't have any such issue. 我没有任何这样的问题。
>>> class A:
def __repr__(self):
return "a"
>>>
>>> A()
a
i think this seems to be your situation: 我认为这似乎是您的情况:
>>> [A(),A()]
[a, a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.