如果要把一个类的实例变成 str,就需要实现特殊方法__str__():

class Node:
 def __init__(self, value):
    self._value = value
    self._children = []
 # def  __repr__(self):
 #     return 'Node({!r})'.format(self._value)
# Example
if __name__ == '__main__':
  root = Node(0)
  print root
  print type(root)


C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a31.py
<__main__.Node instance at 0x01C9BDF0>
<type 'instance'>

如果要把一个类的实例变成 str,就需要实现特殊方法__str__():


class Node:
 def __init__(self, value):
    self._value = value
    self._children = []
 def  __repr__(self):
     return 'Node({!r})'.format(self._value)
# Example
if __name__ == '__main__':
  root = Node(0)
  print root
  print type(root)

C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a31.py
Node(0)
<type 'instance'>

原文地址:https://www.cnblogs.com/hzcya1995/p/13349470.html