0
我想打印其元素是用户定义的类的所有对象的列表,在这个简单的例子列表:打印的元素是类对象
class Athing:
def __init__(self,thething):
self.aray = thething
# Representation of thing for printing
def __str__(self):
return '['+ ', '. join(str(i) for i in self.aray) + ']'
this_thing = []
for j in range(2):
this_thing.append(Athing([[j,j+2,j+3], [j*2,j+5,j+6]]))
print 'this_thing =\n',this_thing
print 'this_thing[0] =\n',this_thing[0]
上面的代码给我下面的结果:
this_thing =
[<__main__.Athing instance at 0x109dec368>, <__main__.Athing instance at 0x109dec3f8>]
this_thing[0] =
[[0, 2, 3], [0, 5, 6]]
为什么不能Athing
对象名单打印出来,而不明确 要求的对象,如第二打印,以及如何才能让 第一个打印的工作?
您还需要定义'__repr__'。 – 2014-09-06 13:52:57