Passing on variable arguments

James T. Dennis jadestar at idiom.com
Sun May 19 04:10:54 EDT 2002


Alex Martelli <aleax at aleax.it> wrote:

> Ralf Juengling wrote:

>> how do I pass on a variable number of arguments. For instance:

> In Python 2.0 and later, you use the * form in the call just as
> in the def statement (in 1.5.2 you had to use builtin function
> 'apply').

>>      def m(self, *args):
>>         # do sth with *args
>>         
>>         # pass them on to superclass method
>>         B.m(self, ????)

>     B.m(self, *args)

> assuming you want to pass ALL of args to B, of course.
> Alex

 It seems we can use slice notation to pass a subset our *args
 

 In python2.2 this seems to work:

	def A(a, *args):
	    print a
	    print len(args), args[:]

	def B(*z):
	    A(z[0], *z[1:])

 ... to pass a subset of the args to decorated/invoked function.
 
 Using apply in this case would still work:

	def C(*args):
	    apply(A, args)

 But it gets uglier if you want to use apply() with a prototype that 
 matches A():

	def D(x, *args):
	    args = list(args)
	    args.insert(0, x)
	    apply(A, args)
  
 I can't see any way around that.  (of course D(), isn't doing anything
 here --- but one could imagine that it might be used to coerce x, 
 perform bounds checking, or perform {pre,post}conditional tests, etc).




More information about the Python-list mailing list