好吧,这个交互式的python控制台代码段将告诉一切:
>>> class Test: ... def __str__(self): ... return 'asd' ... >>> t = Test() >>> print(t) asd >>> l = [Test(), Test(), Test()] >>> print(l) [__main__.Test instance at 0x00CBC1E8, __main__.Test instance at 0x00CBC260, __main__.Test instance at 0x00CBC238]
基本上,我想asd在打印列表时打印三个字符串。我也尝试过,pprint但结果相同。
asd
pprint
尝试:
class Test: def __repr__(self): return 'asd'
并阅读此文档链接: