Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33451
| Date | 2012-11-16 13:30 -0800 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | Re: Is there a simpler way to modify all arguments in a function before using the arguments? |
| References | (3 earlier) <k7ls7f$kvr$1@panix5.panix.com> <eee7cb66-b522-4f5c-bbf4-d33d9429f777@googlegroups.com> <k83vsf$nep$1@ger.gmane.org> <mailman.3731.1353039367.27098.python-list@python.org> <211b323b-1fd4-4018-a7e5-34c6fbda01a5@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3760.1353101765.27098.python-list@python.org> (permalink) |
bruceg113355@gmail.com wrote:
> On Thursday, November 15, 2012 11:16:08 PM UTC-5, Ethan Furman wrote:
>> Emile van Sebille wrote:
>>
>>
>>>> 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?
>>> Extend def wrapper(*args) to handle *kwargs as well
>>> Emile
>>>> Code:
>>>> -----
>>>> from functools import wraps
>>>> def fix_args(fn):
>>>> @wraps(fn)
>>>> def wrapper(*args):
>> so this line ^ becomes
>>
>> def wrapper(*args, **kwargs):
>>
>>>> args = (arg.replace('_', '') for arg in args)
>> and add a line
>>
>> for k, v in kwargs:
>>
>> kwargs[k] = v.replace('_', '')
>>
>>>> return fn(*args)
>> and this line ^ becomes
>>
>> return fn(*args, **kwargs)
>>
>>>> return wrapper
>>
>>
>> ~Ethan~
>
>
> Ethan,
>
> I tried you code suggestions but got errors.
Right, my 'for k, v in kwargs' should have been 'for k, v in kwargs.items()'
Glad you were able to make it work!
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-09 16:48 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Roy Smith <roy@panix.com> - 2012-11-09 20:05 -0500
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-10 01:16 +0000
Re: Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-10 05:15 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Chris Angelico <rosuav@gmail.com> - 2012-11-11 05:33 +1100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Paul Rubin <no.email@nospam.invalid> - 2012-11-09 18:52 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Chris Angelico <rosuav@gmail.com> - 2012-11-10 14:56 +1100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Paul Rubin <no.email@nospam.invalid> - 2012-11-09 20:05 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Chris Angelico <rosuav@gmail.com> - 2012-11-10 20:19 +1100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Miki Tebeka <miki.tebeka@gmail.com> - 2012-11-09 20:17 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Peter Otten <__peter__@web.de> - 2012-11-10 10:09 +0100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? aahz@pythoncraft.com (Aahz) - 2012-11-10 07:35 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-10 09:56 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Peter Otten <__peter__@web.de> - 2012-11-11 10:28 +0100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? brucegoodstein@gmail.com - 2012-11-15 15:20 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Emile van Sebille <emile@fenx.com> - 2012-11-15 16:03 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Ethan Furman <ethan@stoneleaf.us> - 2012-11-15 20:00 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-16 07:00 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Ethan Furman <ethan@stoneleaf.us> - 2012-11-16 13:30 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-16 07:00 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Steve Howell <showell30@yahoo.com> - 2012-11-11 09:45 -0800
csiph-web