def method with variable no of parameters file.writeStuff(n, a1, a2, ...an)

alex23 wuwei23 at gmail.com
Mon Apr 5 01:19:39 CEST 2010


"vlad_fig" <vlad_... at yahoo.com> wrote:
> file.writeStuff(2,a1,a2)
> file.writeStuff(3,a1,a2,a3)
> ....
> file.writeStuff(n,a1,a2,...an)
> ---
> so i want a method i can call based on the number of parameters
> n , and that allows me to add these extra parameters based on n

It's not necessary to have to specify the number of parameters, you
can use Python's support for optional parameters[1] instead:

  def writeStuff(self, *args):
      self.someLines.append(args)

args will be a list of the values passed into writeStuff.

1: http://docs.python.org/reference/expressions.html#calls



More information about the Python-list mailing list