function with a lot of parameters --maintainability issue

Thomas Wouters thomas at xs4all.net
Tue Oct 31 14:47:12 EST 2000


On Tue, Oct 31, 2000 at 07:16:26PM +0000, etsang at my-deja.com wrote:

> I have several functions that takes in more than 255 parameters and
> cause Python to complain about com_addbyte error out of range.
> We are using Python 1.5.2 and have to take that versiona t the moment.

[ .. functions which take more than 255 args)

> So can anyone suggest a better solution at the moment?

Use an argument list or perhaps keyword argument dict instead of naming all
variables, and use 'apply' to call the function:

def somefunc(sid, *args):
	setVariable(sid, ....., args[0])
	setVariable(sid, ....., args[1])
	[....]
	# or depending on your needs, use a for-loop:
	for arg in args:
		setVariable(sid, ...., arg)

apply(somefunc, (a, b, c, d, e, ... zzz))

Of if keyword arguments make more sense (not sure how you are calling the
thing):

def somefunc(sid, *args, **kwargs):
	if args:
		raise ValueError, "need keyword arguments only"
	for key, val in kwargs.items():
		setVariable(sid, ...., key, ..., val)

apply(somefunc, (), {'a': a, 'b': b, 'c': c, .... })

You can get way past 255 arguments that way.  Which method you use and how
you construct the arguments to apply() is entirely up to you. When using
keyword arguments you can do nice things like 

apply(somefunc, (), vars())

or

apply(somefunc, (), dir(myobj))

for instance. The opportunities are endless ;)
	
-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list