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

일을 ...하지만 필요는 방법 "myAppend"호출하지하는 ... 일반 APPEND 방법 이름을 사용하는 것은, 아마도 더 수퍼 클래스를 호출하여 실제 추가를 구현합니다. –