zoukankan      html  css  js  c++  java
  • python 迭代器

    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)
      root.add_child(child1)
      root.add_child(child2)
      for x in root._children:
          print x
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a31.py
    Node(1)
    Node(2)
    
    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)
      root.add_child(child1)
      root.add_child(child2)
      # Outputs Node(1), Node(2)
      for ch in root:
          print(ch)
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a31.py
    Node(1)
    Node(2)
    
    Process finished with exit code 0
    

  • 相关阅读:
    socket注意
    PCM音频文件编码
    题外:分类篇(音乐风格分类)基于BP神经网络
    MFCC特征提取过程详解
    语音信号分析
    k-means聚类
    c++关键字详解
    vs中项目解决方案和项目的关系
    条件编译#ifdef 和#endif
    c++快捷键
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349469.html
Copyright © 2011-2022 走看看