bebraw
12/13/2009 - 12:13 PM

subclassing_a_list.py

# -*- coding: utf-8 -*-
class MyList(list):
  def __init__(self, items, more):
      # modify items here
      new_items = map(lambda x: x + '_hi', items)
      super(MyList, self).__init__(new_items)
    
      # assign rest here
      self.more = more

my_list = MyList(['a', 'b', 'c'], 'stuff')
assert my_list == ['a_hi', 'b_hi', 'c_hi']
assert my_list.more == 'stuff'