Velocity Reviews > Is there a simpler way to modify all arguments in a function beforeusing the arguments?

# Is there a simpler way to modify all arguments in a function beforeusing the arguments?

bruceg113355@gmail.com
Guest
Posts: n/a

 11-10-2012
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
# -----------------
#

Roy Smith
Guest
Posts: n/a

 11-10-2012
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

Steven D'Aprano
Guest
Posts: n/a

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

> In article <(E-Mail Removed)>,
> (E-Mail Removed) 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

Paul Rubin
Guest
Posts: n/a

 11-10-2012
(E-Mail Removed) writes:
> Is there a simpler way to modify all arguments in a function before
> using the arguments?

Why do you want to do that?

> For example, can the below code, in the modify arguments section be
> made into a few statements?

Whenever someone uses that many variables one always has to ask whether
a table would be better. But, for

> 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 (â_â , ââ)

you could write (untested):

def someComputation (aa, bb, cc, dd, ee, ff, gg, hh):
def modify(s): return s.replace('_', '')
aa,bb,cc,dd,ee,ff,gg,hh = \
map(modify,[aa,bb,cc,dd,ee,ff,gg,hh])

Chris Angelico
Guest
Posts: n/a

 11-10-2012
On Sat, Nov 10, 2012 at 1:52 PM, Paul Rubin <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
>> Is there a simpler way to modify all arguments in a function before
>> using the arguments?

>
> Why do you want to do that?
>

Contrived example:

def send_email(from, to, subj, body, whatever, other, headers, you, like):
# Okay, now translate all those into the appropriate encoding and
with special characters escaped
# We need to translate each one separately so that, for instance,

ChrisA

Paul Rubin
Guest
Posts: n/a

 11-10-2012
Chris Angelico <(E-Mail Removed)> writes:
> Contrived example:
> def send_email(from, to, subj, body, whatever, other, headers, you, like):

That should be a dictionary with the header names as indexes. In fact
there are already some email handling modules in the stdlib that

Miki Tebeka
Guest
Posts: n/a

 11-10-2012
> Is there a simpler way to modify all arguments in a function before using the arguments?
You can use a decorator:

from functools import wraps

def fix_args(fn):
@wraps(fn)
def wrapper(*args):
args = (arg.replace('_', '') for arg in args)
return fn(*args)

return wrapper

@fix_args
def foo(x, y):
print(x)
print(y)

Peter Otten
Guest
Posts: n/a

 11-10-2012
Miki Tebeka wrote:

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

> You can use a decorator:
>
> from functools import wraps
>
> def fix_args(fn):
> @wraps(fn)
> def wrapper(*args):
> args = (arg.replace('_', '') for arg in args)
> return fn(*args)
>
> return wrapper
>
> @fix_args
> def foo(x, y):
> print(x)
> print(y)

I was tempted to post that myself, but he said /simpler/

Chris Angelico
Guest
Posts: n/a

 11-10-2012
On Sat, Nov 10, 2012 at 3:05 PM, Paul Rubin <(E-Mail Removed)> wrote:
> Chris Angelico <(E-Mail Removed)> writes:
>> Contrived example:
>> def send_email(from, to, subj, body, whatever, other, headers, you, like):

>
> That should be a dictionary with the header names as indexes. In fact
> there are already some email handling modules in the stdlib that

That's also plausible, but keyword arguments do make sense. And this
was a top-of-the-head contrived example; I'm sure there are plenty of
good use-cases.

ChrisA

bruceg113355@gmail.com
Guest
Posts: n/a

 11-10-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 <(E-Mail Removed)>,

>
> > (E-Mail Removed) 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