zoukankan      html  css  js  c++  java
  • 4.2 代理迭代:

    4.2 代理迭代:
    
    你构建了一个自定义容器对象,里面包含了列表,元组或其他可迭代对象。
    
    class Node:
      def __init__(self, value):
        self._value = value
        self._children = []
    # Example
    if __name__ == '__main__':
       root = Node(0)
       print root
       print type(root)
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a36.py
    <__main__.Node instance at 0x01D9BDF0>
    <type 'instance'>
    
    
    将实例转换成字符串:
    
    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/a36.py
    Node(0)
    <type 'instance'>
    
    Process finished with exit code 0
    
    
    class Node:
      def __init__(self, value):
        self._value = value
        self._children = []
    
      # def __repr__(self):
      #     return 'Node({!r})'.format(self._value)
    
      def add_child(self, node):
          self._children.append(node)
    # Example
    if __name__ == '__main__':
       root = Node(0)
       print root
       print type(root)
       child1 = Node(1)
       child2 = Node(2)
       print '--------------'
       print child1
       print child2
       root.add_child(child1)
       root.add_child(child2)
       print root._children
       print '1111111111111'
       for x in root._children:
           print x
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a36.py
    <__main__.Node instance at 0x024DF940>
    <type 'instance'>
    --------------
    <__main__.Node instance at 0x024CF0D0>
    <__main__.Node instance at 0x02513DF0>
    [<__main__.Node instance at 0x024CF0D0>, <__main__.Node instance at 0x02513DF0>]
    1111111111111
    <__main__.Node instance at 0x024CF0D0>
    <__main__.Node instance at 0x02513DF0>
    
    Process finished with exit code 0
    
    
    class Node:
      def __init__(self, value):
        self._value = value
        self._children = []
    
      # def __repr__(self):
      #     return 'Node({!r})'.format(self._value)
    
      def add_child(self, node):
          self._children.append(node)
    
      def __iter__(self):
          return iter(self._children)
    # Example
    if __name__ == '__main__':
       root = Node(0)
       print root
       print type(root)
       child1 = Node(1)
       child2 = Node(2)
       print '--------------'
       print child1
       print child2
       root.add_child(child1)
       root.add_child(child2)
       print root._children
       print '1111111111111'
       for x in root:
           print x
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a36.py
    <__main__.Node instance at 0x02733DF0>
    <type 'instance'>
    --------------
    <__main__.Node instance at 0x0273C3A0>
    <__main__.Node instance at 0x0273C3C8>
    [<__main__.Node instance at 0x0273C3A0>, <__main__.Node instance at 0x0273C3C8>]
    1111111111111
    <__main__.Node instance at 0x0273C3A0>
    <__main__.Node instance at 0x0273C3C8>

  • 相关阅读:
    云游四海
    保持良好的人际关系,赢得好人缘的八大诀窍
    二十三格经典的管理定律(建议收藏)
    游北湖公园有感
    如何成为领袖? 学习任正非小沃森郭士纳
    梦回江南
    观野花展有感
    爱一个人要爱多久
    醉卧山林
    游环岛路有感
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349467.html
Copyright © 2011-2022 走看看