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 18:56:04 CET 2012


All,

I never used decorators before. I saw Miki Tebeka's sample code and your rationale (Aahz) and I like it. For my application problem, decorators seem like a good solution.

Thanks to all,
Bruce




On Saturday, November 10, 2012 10:35:12 AM UTC-5, Aahz wrote:
> In article <mailman.3530.1352538537.27098.python-list at python.org>,
> 
> Peter Otten  <__peter__ at 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 (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/
> 
> 
> 
> "....Normal is what cuts off your sixth finger and your tail..."  --Siobhan



More information about the Python-list mailing list