calling superclass' method with list positional arg
Steven Haryanto
steven at haryan.to
Fri Apr 13 04:44:10 EDT 2001
My Bag's constructor accepts a list positional args to fill
the object with initial data, so I can conveniently create
a bag initially filled with stuff like this:
mybag = Bag("apples", "oranges", "money")
BagOTricks is a subclass of Bag, and it needs to do something
else but let the superclass do the actual data filling.
Currently I do it like this:
class Bag:
def __init__(self, *items):
self._items = list(items)
def add(self, item):
self._items.append(item)
class BagOTricks(Bag):
def __init__(*args):
self = args_[0]
# do something else first...
# then pass the items to superclass' constructor
apply(Bag.__init__, args_)
Is there an elegant way to do this so I can still declare
Bag's __init__ as 'def __init__(self, *items)', but I don't
need to create a temporary list like below?
class BagOTricks(Bag):
def __init__(self, *items):
# do something else first...
# then pass the items to superclass' constructor
args = list(items)
args.insert(0, self)
apply(Bag.__init__, args)
Thanks,
Steve
More information about the Python-list
mailing list