2010年9月25日土曜日

インスタンスをリストに持たせる。

複数のインスタンスを作った場合に、管理するのに便利そう。

http://blogs.yahoo.co.jp/nanashi_hippie/47503485.html


  1. class hello:  
  2.     def __init__(self, num):  
  3.         self.num = num  
  4.         print 'hello init'  
  5.       
  6.     def output(self):  
  7.         print 'hello output'self.num  
  8.   
  9.   
  10. def main():  
  11.     pack = []  
  12.       
  13.     test1 = hello(5)  
  14.     test2 = hello(10)  
  15.       
  16.     pack.append(test1)  
  17.     pack.append(test2)  
  18.       
  19.     print pack  
  20.     for x in [0,1]:  
  21.         pack[x].output()  
  22.       
  23.     pack.pop(0)  
  24.     pack[0].output()  
  25.       
  26.     return  
  27.   
  28. if __name__ == '__main__': main()