function with a lot of parameters --maintainability issue
William Park
parkw at better.net
Tue Oct 31 15:13:01 EST 2000
On Tue, Oct 31, 2000 at 07:16:26PM +0000, etsang at my-deja.com wrote:
> Hi all,
>
> 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.
>
> I think one way to get around that is as follows:
>
> Since a lot of your functions are like:
> def func(sid,a,b,c,d,e,f, ..... more than 255 parameters):
>
> setVariable(sid,.....,a);
> setVariable(sid,.....,b);
> .....
> setVariable(sid,.....,z);
> ......
> sendPrim(sid,"somePrim ..");
> we can break the function into several functions:
>
> def func1(sid,a,b,c,d,e):
> setVariable(sid,.....,a);
> setVariable(sid,.....,b);
> ....
> setVariable(sid,.....,d);
> setVariable(sid,.....,e);
> def func2(sid,f,g,h,i,....):
> setVariable(sid,.....,f);
> setVariable(sid,.....,g);
> ....
>
> def func3(sid,......):
> setVariable(sid, ....);
> sendPrim(sid,........);
> Note that setVariable and sendPrim are just other user defined
> functions.
>
> Then just call these three functions in order of func1,func2,func3.
>
> That is in places that call func, that following is replaced
> func1, func2, func3. There are quite a lot of places that call func.
> So this may generate maintainability issue, as we need to keep track of
> three functions instead of one.
>
> So can anyone suggest a better solution at the moment?
Yes, pass dictionary to the function, ie.
def func(sid, args):
for i in args.keys():
setVariable(sid, ..., args[i]) # argv['a'], ...
argv = {'a':..., 'b':..., 'c':..., ...}
func(sid, argv)
---William Park, Open Geometry Consulting
More information about the Python-list
mailing list