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>

  • 相关阅读:
    PHPEXCEL 导出多个sheet
    android adb.exe端口占用
    imageview 显示assets的图片

    Java中日期问题
    java中的定时器的实现样例
    Oracle数据库如何干净的删除
    MySQL索引相关知识
    JavaScript基础知识总结
    JDBC技术总结
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349467.html
Copyright © 2011-2022 走看看