Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33124

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'args': 0.04; 'aahz': 0.05; 'modify': 0.05; "'')": 0.07; 'args)': 0.07; 'arguments': 0.07; 'decorator': 0.07; 'wrapper': 0.07; '*is*': 0.09; 'arg': 0.09; 'foo,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'wraps': 0.09; 'def': 0.10; 'functools': 0.16; 'pov,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'simpler,': 0.16; 'tempted': 0.16; 'y):': 0.16; 'wrote:': 0.17; 'helper': 0.17; 'typical': 0.17; '>>>': 0.18; 'changes': 0.20; 'parameters': 0.20; 'import': 0.21; 'latter': 0.22; 'simpler': 0.22; 'allows': 0.25; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'post': 0.28; '>>>>': 0.29; 'subject:all': 0.29; 'probably': 0.29; 'becomes': 0.30; 'function': 0.30; 'expect': 0.31; 'code': 0.31; 'maintained': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'produced': 0.33; 'subject:?': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'client': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'kind': 0.61; 'needing': 0.62; 'more': 0.63; 'subject:there': 0.65; 'overall': 0.66; 'obvious': 0.71; 'article': 0.78; 'complexity': 0.84; 'otten': 0.84; 'subject:before': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Is there a simpler way to modify all arguments in a function before using the arguments?
Date Sun, 11 Nov 2012 10:28:55 +0100
Organization None
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>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084948f.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3556.1352626145.27098.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352626145 news.xs4all.nl 6969 [2001:888:2000:d::a6]:54330
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33124

Show key headers only | View raw


Aahz wrote:

> In article <mailman.3530.1352538537.27098.python-list@python.org>,
> Peter Otten  <__peter__@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.

Typical changes would be

@fix_args
def bar(x, y=None):
    print(x)
    print(y)

@fix_args
def baz(file, x, y):
    print(s, file=file)

Do you find it obvious what 

bar("a_b")
bar("a_b", y="c_d")

print? Do you find the traceback produced by the latter helpful?
Moving complexity into a helper function often makes client code simpler 
because if the helper is well-tested and preferrably maintained by someone 
else the part that you have to deal with becomes simpler, but the overall 
complexity still increases.
A fix_args() decorator is worthwhile only if you need it more than once or 
twice, and because it is hard to generalise I expect that yagni.



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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