阅读背景:

如果要把一个类的实例变成 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:\Python27\python.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:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a31.py
Node(0)
<type 'instance'>class Node:
 def __init__(self, value):
    sel



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: