您的位置:首页 > 其它

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

2017-10-30 17:24 253 查看
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'>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐