Message from discussion
Is there a simpler way to modify all arguments in a function before using the arguments?
Received: by 10.224.189.16 with SMTP id dc16mr1965530qab.0.1353021610596;
Thu, 15 Nov 2012 15:20:10 -0800 (PST)
Received: by 10.49.58.167 with SMTP id s7mr628817qeq.5.1353021610581; Thu, 15
Nov 2012 15:20:10 -0800 (PST)
Path: gf5ni327qab.0!nntp.google.com!u2no297915qal.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Thu, 15 Nov 2012 15:20:10 -0800 (PST)
In-Reply-To: <k7ls7f$kvr$1@panix5.panix.com>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.49.62.250;
posting-account=_prvmgoAAACcjzrWP0DkuKYXjTjeuFfN
NNTP-Posting-Host: 173.49.62.250
References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com>
<a8ed6936-0e85-421b-a2a4-96f255e7d76d@googlegroups.com> <mailman.3530.1352538537.27098.python-list@python.org>
<k7ls7f$kvr$1@panix5.panix.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <eee7cb66-b522-4f5c-bbf4-d33d9429f777@googlegroups.com>
Subject: Re: Is there a simpler way to modify all arguments in a function
before using the arguments?
From: brucegoodst...@gmail.com
Injection-Date: Thu, 15 Nov 2012 23:20:10 +0000
Content-Type: text/plain; charset=ISO-8859-1
On Saturday, November 10, 2012 10:35:12 AM UTC-5, Aahz wrote:
> In article <mailman.3530.1352538537.27098.python-l...@python.org>,
>
> Peter Otten <__pete...@web.de> wrote:
>
> >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/ ;)
>
>
>
> From my POV, that *is* simpler. When you change the parameters for foo,
>
> you don't need to change the arg pre-processing. Also allows code reuse,
>
> probably any program needing this kind of processing once will need it
>
> again.
>
> --
>
> Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/
>
>
>
> "....Normal is what cuts off your sixth finger and your tail..." --Siobhan
Using a decorator works when named arguments are not used. When named arguments are used, unexpected keyword error is reported. Is there a simple fix?
Thanks to all,
Bruce
Code:
-----
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(a1="", a2="", b1="", b2=""):
print(a1)
print(a2)
print(b1)
print(b2)
foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x')
foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x')
Results:
--------
a1a1x
a2a2x
b1b1x
b2b2x
Traceback (most recent call last):
File "C:\WORK\masterDB_Update\argtest.py", line 19, in <module>
foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x')
TypeError: wrapper() got an unexpected keyword argument 'a1'