Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
 
Ethan Furman
Guest
Posts: n/a
 
      11-16-2012
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~
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a simpler way to do this? Ritchie Valens Javascript 6 08-16-2011 09:23 PM
Is there a better/simpler way to filter blank lines? tmallen Python 19 11-05-2008 11:07 PM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
Is there a simpler way to do this? Julian Leviston Ruby 12 08-27-2005 11:16 PM
Is there a simpler way (trick) to create composite controls ? Alex Nitulescu ASP .Net 5 03-03-2005 11:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57