说我有一个元素类print()函数是如何工作的?
class Element(object):
def __init__(self,key,val):
self.key = key
self.val = val
def __str__(self):
return '(' + str(self.key) + ',' + str(self.val) + ')'
现在,当我尝试打印包含Element类
arr = [Element(10,20),Element(20,30)]
print(arr)
的对象的数组,输出为 -
[<maxheap.Element object at 0x01C1FCB0>, <maxheap.Element object at 0x01C270B0>]
哪功能正在打印<maxheap.Element object at 0x01C1FCB0>
?为什么不在print(Element(10,20))
中调用__str__(self)
方法?
[Python中的\ _ \ _ str \ _ \ _和\ _ \ _ repr \ _ \ _之间的区别]的可能重复(http://stackoverflow.com/questions/1436703/difference-between-str-and- python) – mkrieger1