How to call a method with variing arguments

Roman Suzi rnd at onego.ru
Tue Jul 3 03:05:17 EDT 2001


On 2 Jul 2001, Heiko wrote:

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

...

Works like this:

>>> 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])
default def2 [1, 2, 3]
>>> class_object.Method("a", "b", [2,3,4])
a b [2, 3, 4]
>>>


> Thanks
> Heiko
> 

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list