function with a lot of parameters --maintainability issue
Alex Martelli
aleaxit at yahoo.com
Thu Nov 2 06:44:05 EST 2000
"Thomas A. Bryan" <tbryan at python.net> wrote in message
news:3A0002E2.B2F24058 at python.net...
[snip]
> A class might be cleaner, depending on what you're doing. Another
> approach that might be simpler is to use keyword arguments.
Yes, but you're not using keyword-argument syntax sugar in the
following example:
> In blarblar.py,
> # Define it here so that func isn't so ugly
> configDict = ['a': 1, 'b': TRUE,...]
>
> def func(sid, dictArg):
Change this to
def func(sid, **dictArg):
to have func take keyword-arguments.
> 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})
Change the call to
func(sid, a=1, b=FALSE)
if keyword-argument syntax is wanted.
> def callTwo():
> func(sid, {'c': 'Non default'})
Ditto,
func(sid, c='Non default')
> # Now, it doesn't matter whether callYou() has been called
> callTwo()
Yes, this is an important semantic characteristic of your
solution. I'm just showing the little syntax-sugar changes
that are needed to it if one does want to use keyword
arguments -- it works just the same way!-)
Alex
More information about the Python-list
mailing list