代码之家  ›  专栏  ›  技术社区  ›  Xolve

将事件添加到列表

  •  2
  • Xolve  · 技术社区  · 17 年前

    我该怎么做?

    1 回复  |  直到 17 年前
        1
  •  1
  •   Jason Coon    17 年前

    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']