• 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>

  • 相关阅读:
    微信小程序在sublime开发代码高亮显示
    CSS之flex兼容
    本地存储(2)
    IE浏览器兼容性问题解决方案
    设计一套方案,解决不同浏览器的兼容问题(2)
    Webpack, 现在最流行的模块打包工具.压缩打包
    Linux环境下安装配置Node.js
    阿里云服务器 linux 怎么安装php(PHPSTUDY)开发环境
    函数与闭包
    内建的控制结构
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349467.html
Copyright © 2020-2023  润新知