2009-04-01 4 views
1

アイテムを追加すると、そのアイテムに基づいてアクションが実行されるようなイベントをリストに追加します。新しいデータ構造の生成、画面出力の変更、または例外の発生。リストにイベントを追加する

どうすればよいですか?

答えて

1

あなたは、リストオブジェクト拡張する独自のクラスを作成できます。

class myList(list): 
    def myAppend(self, item): 
     if isinstance(item, list): 
      print 'Appending a list' 
      self.append(item) 
     elif isinstance(item, str): 
      print 'Appending a string item' 
      self.append(item) 
     else: 
      raise Exception 

L = myList() 
L.myAppend([1,2,3]) 
L.myAppend('one two three') 
print L 

#Output: 
#Appending a list 
#Appending a string item 
#[[1, 2, 3], 'one two three'] 
+0

+1 ...しかし、メソッド「myAppend」をコールする必要はありませんが...通常のappendメソッド名を使用すると、おそらく優れています、スーパークラスを追加して実際の追加を実装するだけです –

関連する問題