function with a lot of parameters --maintainability issue

Thomas A. Bryan tbryan at python.net
Wed Nov 1 06:47:46 EST 2000


William Park wrote:
> 
> On Mon, Oct 30, 2000 at 03:30:19PM -0800, First Name Last Name wrote:
> > William, thanks for your help. One more quesiton about your solution.
> > Since I would like that dictionary to contain a whole set of default values of different types. Can I do something like this:
> >
> > In file blarblar.py  where func is definied, I also define the dictionary.
> > That is:
> > configDict = {'a': 1, 'b': TRUE, 'c': "Default Val", 'd': "1.245", ...}
> >
> > def func(sid, args):
> >     for i in args.keys():
> >      setVariable(sid, ..., args[i])
> >
> > In the file that will call the function
> > from blarblar import *
> >
> > def callYou():
> >     # assume sid is got from somewhere
> >     newConfigDict = configDict
> >     # want to change some of the default values
> >     newconfigDict['a'] = 2
> >     newConffigDict['b'] = FALSE
> >     func(sid,newConfigDict)
> > ????
> 
> Yes, something like that.  But, since you already have the dictionary
> defined in 'blarblar.py', there is no need to pass it to
> 'blarblar.func()'.  Also, 'newConfigDict' is not necessary.
>
> In 'blarblar.py',
>     configDict = {'a': 1, 'b': TRUE, ...}
> 
>     def func(sid):
>         for i in configDict.keys():
>             setVariable(sid, ..., configDict[i])
> 
> And in your main script,
>     from blarblar import *
> 
>     def callYou():
>         ...
>         configDict['a'] = 2
>         configDict['b'] = FALSE
>         func(sid)

The problem with this solution is that if you have a second function
(or later *add* a second function) to the main script that calls 
func(), then the arguments that are used depend on the order of the 
function calls.

For example, 
In blarblar.py
configDict = ['a': 1, 'b': TRUE,...]

def func(sid):
    for i in configDict.keys():
        setVariable(sid, ... , configDict[i])

And in the main script, 
from blarblar import *

def callYou():
    configDict['a'] = 2
    func(sid)

def callTwo():
    configDict['b'] = FALSE
    func(sid)

# ... a bunch of code that might call callYou()
callTwo()  # what happens depends on whether callYou() has been called

> > Or I am think should I implement this configDict stuff in a class instead???
> 
> Hmm... try dictionary first.  Then, when you are comfortable with
> Python, then perhaps class.  Either will work.

A class might be cleaner, depending on what you're doing.  Another 
approach that might be simpler is to use keyword arguments.

In blarblar.py, 
# Define it here so that func isn't so ugly
configDict = ['a': 1, 'b': TRUE,...]

def func(sid, dictArg):
    myDict = configDict.copy()  # use a copy to avoid corrupting the global
    myDict.update(dictArg)      # override defaults with values passed to us
    for i in myDict.keys():
        setVariable(sid, ... , myDict[i])

In the main program, 
from blarblar import *

def callYou():
    func(sid, {'a': 1, 'b': FALSE})

def callTwo():
    func(sid, {'c': 'Non default'})

# Now, it doesn't matter whether callYou() has been called 
callTwo()

---Tom



More information about the Python-list mailing list