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

bruceg113355 at gmail.com bruceg113355 at gmail.com
Sat Nov 10 08:15:21 EST 2012


On Friday, November 9, 2012 8:16:12 PM UTC-5, Steven D'Aprano wrote:
> 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


Thanks to all. 
Steve's example is the one I will try next week. 
Passing in lists, will work but it requires extra coding from the calling routines to build the list.
Discrete arguments make sense. 
Also, what is the problem passing in 7 or more arguments?

Thanks,
Bruce



More information about the Python-list mailing list