[Python-Help] Calling superclass method with variable args?

Steve Johnson slj at pixar.com
Thu Jul 15 15:01:11 EDT 1999


Hi Jim,

You're a God!  I thought I'd tried that, but trying it now, it does indeed
seem to work.

I don't understand why morekeyargs doesn't become a single dictionary
argument to Task.__init__,.  I'll read the FAQ.

Thanks very much!

Steve

> Steve Johnson wrote:
> >
> > Hi,
> >
> > I'm a long-time C++ programmer, but I'm really new to Python, so please
> > forgive me if this is a dumb question with a simple or obvious answer.
> >
> > A fundamental idea in OOP is the ability to have a subclass augment the
> > behavior of a superclass's method by redefine that method, and then
> > calling the superclass's version of the method before doing anything
> > else.  The key to this idea, and to polymorphism in general, is that the
> > two methods have the same argument signature so that the superclass's
> > method gets passed the same set of arguments from the caller whether the
> > subclass overrides the method or not.
> >
> > If these methods have a static argument list, this is an easy thing to
> > do in Python. Here's an example:
> >
> > class Task:
> >     def __init__(self,a,b,c):
> >         <whatever>
> >
> > class Job(Task):
> >     def __init__(self,a,b,c):
> >         Task.__init__(self,a,b,c)
> >         <whatever>
> >
> > aJob = Job(x,y,z);
> >
> > But what if the method takes unnamed positional and keyword arguments.
> > Then, how do you pass these along to the superclass's version of the
> > method such that it gets the arguments in the same form that the
> > subclass's method did?  Here's the code in question:
> >
> > class Task:
> >     def __init__(self,a,b,c,*moreargs,**morekeyargs):
> >         <whatever>
> >
> > class Job(Task):
> >     def __init__(self,a,b,c, *moreargs, **morekeyargs):
> >         Task.__init__(self,a,b,c,?????)
> >         <whatever>
> >
> > aJob = Job(x,y,z,m1,m2,m3,m4,m5);
> >
> > My question really is...what do I replace the ????? (or that whole line)
> > with to get this fundamental OOP behavior?
> >
> > This doesn't work:
> >
> >         Task.__init__(self,a,b,c,moreargs,morekeyargs)
> >
> > because now Task.__init__ will see exactly 5 arguments, instead of some
> > arbitrary number.  This almost works:
> >
> >         apply(Task.__init__,(self,a,b,c) + moreargs)
> >
> > but the extra keyword arguments don't get passed along. So, I took the
> > idea one step further and tried to introduce the keyword arguments back
> > into the parameter list that gets passed to apply:
>
> This is addressed by FAQ 4.76:
>
>   http://www.python.org/doc/FAQ.html#4.76
>
> you want: apply(Task.__init__,(self,a,b,c) + moreargs, morekeyargs)
>
> Jim
>
> --
> Jim Fulton           mailto:jim at digicool.com   Python Powered!
> Technical Director   (888) 344-4332            http://www.python.org
> Digital Creations    http://www.digicool.com   http://www.zope.org
>
> Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
> address may not be added to any commercial mail list with out my
> permission.  Violation of my privacy with advertising or SPAM will
> result in a suit for a MINIMUM of $500 damages/incident, $1500 for
> repeats.





More information about the Python-list mailing list