calling superclass' method with list positional arg

Jeremy Hylton jeremy at digicool.com
Fri Apr 13 18:14:04 EDT 2001


>>>>> "SH" == Steven Haryanto <steven at haryan.to> writes:

  SH> class Bag:

  SH>      def __init__(self, *items):
  SH>          self._items = list(items)

  SH>      def add(self, item):
  SH>          self._items.append(item)

One possibility:

  SH> class BagOTricks(Bag):

  SH>      def __init__(*args):
  SH>          self = args_[0]
  SH>          # do something else first...

  SH>          # then pass the items to superclass' constructor
  SH>          apply(Bag.__init__, args_)

  SH> Is there an elegant way to do this so I can still declare Bag's
  SH> __init__ as 'def __init__(self, *items)', but I don't need to
  SH> create a temporary list like below?

Another possibility:

  SH> class BagOTricks(Bag):

  SH>      def __init__(self, *items):
  SH>          # do something else first...

  SH>          # then pass the items to superclass' constructor
  SH>          args = list(items) args.insert(0, self)
  SH>          apply(Bag.__init__, args)

I would do it this way:

class BagOTricks(Bag):
    __super_init = Bag.__init__

    def __init__(self, *items):
        # do something
        self.__super_init(*items)

Jeremy







More information about the Python-list mailing list