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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Nov 9 20:16:12 EST 2012


On Fri, 09 Nov 2012 20:05:26 -0500, Roy Smith wrote:

> In article <18134e77-9b02-4aec-afb0-794ed900d194 at googlegroups.com>,
>  bruceg113355 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
>>        # -----------------
>>        # Š
> 
> You could do something like (not error checked)...
> 
> def someComputation(*args):
>     new_args = [arg.replace("_", "") for arg in args] aa, bb, cc, dd,
>     ee, ff, gg, hh = new_args
> 
> but that's pretty weird.  I suspect you just want to pass a list instead
> of a bunch of discrete arguments.


I agree with everything you say except that it is pretty weird. As far as 
I am concerned, it isn't weird at all.

If you need named parameters:

def someComputation(aa, bb, cc, dd, ee, ff, gg, hh):
    aa, bb, cc, dd, ee, ff, gg, hh = [arg.replace("_", "") 
            for arg in (aa. bb, cc, dd, ee, ff, gg, hh)]
    ...



-- 
Steven



More information about the Python-list mailing list