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

V

vlad_fig

Hello all,

I would like some help with setting up a method that would allow me to change its number of parameters. For example:

#---------------------
class createfile(object):

def __init__(self,
modelName = None,
someLines = None):

self.modelName = modelName

if someLines is None:
self.someLines = []
else:
self.someLines = someLines

def writeStuff(self,
numberParameters = None,
Parameter1 = None,... ??? )
self.someLines .append("yes, we can %s" % self.Parameter1)
#---------------------
file = createfile('file')

file.writeStuff(2,a1,a2)
file.writeStuff(3,a1,a2,a3)
.....
file.writeStuff(n,a1,a2,...an)
 
J

Joaquin Abian

Hello all,

I would like some help with setting up a method that would allow me to change its number of parameters. For example:

#---------------------
class createfile(object):

def __init__(self,
modelName = None,
someLines = None):

self.modelName = modelName

if someLines is None:
self.someLines = []
else:
self.someLines = someLines

def writeStuff(self,
numberParameters = None,
Parameter1 = None,... ??? )
self.someLines .append("yes, we can %s" % self.Parameter1)
#---------------------
file = createfile('file')

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

Thank you,
Vicnic

Maybe you are looking for *args:

class Test():
def meth(self, *args):
print args

inst = Test()
inst.meth(1,2,3,4,5,6,7)

#prints (1,2,3,4,5,6,7)

Cheers
Joaquin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top