Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; "'')": 0.07; 'args)': 0.07; 'arguments': 0.07; 'decorator': 0.07; 'used.': 0.07; 'work!': 0.07; 'wrapper': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'arg': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'subject:using': 0.09; 'unexpected': 0.09; 'wraps': 0.09; '~ethan~': 0.09; 'def': 0.10; 'functools': 0.16; 'received:69.93': 0.16; 'received:69.93.243': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'import': 0.21; '15,': 0.23; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'extend': 0.26; 'used,': 0.27; 'errors.': 0.27; '>>>>': 0.29; 'subject:all': 0.29; 'van': 0.29; 'becomes': 0.30; 'keyword': 0.30; 'thursday,': 0.30; 'error': 0.30; 'code': 0.31; '-----': 0.32; 'handle': 0.33; 'to:addr :python-list': 0.33; 'code:': 0.33; 'subject:?': 0.35; 'there': 0.35; 'add': 0.36; 'but': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'email addr:gmail.com': 0.63; 'subject:there': 0.65; 'ethan,': 0.84; 'furman': 0.84; 'subject:before': 0.84; 'glad': 0.86; 'ethan': 0.91 Date: Fri, 16 Nov 2012 13:30:34 -0800 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: python-list@python.org Subject: Re: Is there a simpler way to modify all arguments in a function before using the arguments? References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> <211b323b-1fd4-4018-a7e5-34c6fbda01a5@googlegroups.com> In-Reply-To: <211b323b-1fd4-4018-a7e5-34c6fbda01a5@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: yes X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([192.168.11.101]) [173.12.184.238]:50841 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 0 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353101765 news.xs4all.nl 6977 [2001:888:2000:d::a6]:33846 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33451 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~