Python 打印元素为类对象的列表

Python 打印元素为类对象的列表,python,object,printing,Python,Object,Printing,我试图打印一个列表,其元素都是用户定义类的对象,如下面的简单示例所示: 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

我试图打印一个列表,其元素都是用户定义类的对象,如下面的简单示例所示:

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]]
这件事= [, ] 这个东西[0]= [[0, 2, 3], [0, 5, 6]] 为什么
Athing
对象的列表不能在没有明确显示的情况下打印它们 如第二次打印中所示,询问对象,我如何制作
第一次打印工作?

打印包含对象的列表时,对列表本身使用
\uuuuuu str\uuuuu
,但对
列表的实现。
为您的案例中的每个对象调用
\uuu repr\uuuu

因此,您希望覆盖
\uuuuu repr\uuuu
,而不必覆盖
\uuuu str\uuuu
,因为:

如果类定义了repr(),但没有定义str(),则repr()是 也可在以下情况下使用:“非正式”字符串表示 那门课是必修课


您还需要定义
\uuu repr\uu