How to call a method with variing arguments
Delaney, Timothy
tdelaney at avaya.com
Tue Jul 3 03:18:13 EDT 2001
> > I want to do this:
> >
> > ClassObject = Class()
> > ClassObject.Method(Arg1, Arg2, Arg3)
> > ...
> > ClassObject.Method(Arg1, Arg2, Arg3, Arg4, Arg5 ....)
> > ...
> > ClassObject.Method()
> > ...
> > and so on.
> >
> > Means that I want to call a method with a variing number of
> arguments
> > and the method decides what to do.
> > If possible, it would be nice, if the sequence of the arguments
> > doesn│t matter, and the method realizes which argument is on which
> > position (but that is a "nice to have"). So, how do I have
> to declare
> > the method, that this works?
>
> example:
>
> class Class:
> def Method(self, Arg1="default", Arg2="def2", Arg3=None):
> Arg3 = Arg3 or []
> print Arg1, Arg2, Arg3
>
> class_object = Class()
> class_object.Method(Arg3=[1,2,3])
> class_object.Method("a", "b", [2,3,4])
Actually, I suspect he wants to be able to pass an arbitrary number of
parameters ...
class ClassObject:
def Method (self, *args):
# args is a tuple of the arguments passed in
print args
for a in args:
print a
c = ClassObject()
c.Method()
print
c.Method(1)
print
c.Method(1, 2)
print
c.Method(1, 2, 3)
print
Tim Delaney
More information about the Python-list
mailing list