How to call a method with variing arguments

Alex Martelli aleaxit at yahoo.com
Tue Jul 3 04:08:10 EDT 2001


"Heiko" <meinmailfilter at gmx.de> wrote in message
news:e956b5f1.0107022240.776c1734 at posting.google.com...
> Hello,
> I want to do this:
>
> ClassObject = Class()
> ClassObject.Method(Arg1, Arg2, Arg3)
> ...
> ClassObject.Method(Arg1, Arg2, Arg3, Arg4, Arg5 ....)
> ...
> ClassObject.Method()
> ...
> and so on.

Well, then, just do it!  No problem.


> Means that I want to call a method with a variing number of arguments
> and the method decides what to do.

Sure, Python supports that.

> 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").

This one I really don't understand.  Anyway, once you do have
a sequence of arguments (which is what you get), you may well
decide to ignore the sequence's order and treat it as "a set".

> So, how do I have to declare
> the method, that this works?

class Class:
    def Method(self, *args):
        print "Called with",len(args),"arguments"

args is the tuple of actual argument values passed on the
call -- it's up to the method to decide what to do with them.


Alex






More information about the Python-list mailing list