Is there a simpler way to modify all arguments in a function before using the arguments?

Steve Howell showell30 at yahoo.com
Sun Nov 11 12:45:50 EST 2012


On Nov 9, 4:48 pm, bruceg113... at gmail.com wrote:
> Is there a simpler way to modify all arguments in a function before using the arguments?
>
> For example, can the below code, in the modify arguments section be made into a few statements?
>
>     def someComputation (aa, bb, cc, dd, ee, ff, gg, hh):
>        # modify arguments
>        # ----------------------
>         aa = aa.replace (“_” , “”)
>         bb=  bb.replace (“_” , “”)
>         cc = cc.replace (“_” , “”)
>         dd = dd.replace (“_” , “”)
>         ee = ee.replace (“_” , “”)
>         ff = ff.replace (“_” , “”)
>         gg = gg.replace (“_” , “”)
>         hh = hh.replace (“_” , “”)
>
>        # use the arguments
>        # -----------------
>        # …

I would couch this problem in a little more specific terms than trying
to make this "simpler."

The word "simple" is a dangerous term, because it's so broad and
subjective.  By my mind, the code is already simple, but that's just
my own two cents.

The real problem with the code that it's a maintenance trap, because a
careless developer could add the ii parameter and forget to clean the
output.  So the problem statement here might be more like "How do I
make sure future developers don't forget to fix the underscores in
future args?".  That's still a controversial question, but at least
it's a little more specific.

The other problem with the current code is that all the boilerplate
distracts from the real logic of the function.  That's a valid
concern, although it's likely that most maintainers of the code would
simply page down past the boilerplate without too much complaint.



More information about the Python-list mailing list