Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.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; 'wrapper': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'arg': 0.09; 'subject:using': 0.09; 'to:addr:comp.lang.python': 0.09; 'unexpected': 0.09; 'wraps': 0.09; '~ethan~': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'functools': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.17; 'import': 0.21; 'cc:2**0': 0.23; '15,': 0.23; 'bruce': 0.23; 'cc:no real name:2**0': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'extend': 0.26; 'used,': 0.27; 'errors.': 0.27; 'subject:all': 0.29; 'van': 0.29; "skip:' 10": 0.30; 'becomes': 0.30; 'keyword': 0.30; 'thursday,': 0.30; 'error': 0.30; 'code': 0.31; '-----': 0.32; 'print': 0.32; 'handle': 0.33; 'code:': 0.33; 'received:google.com': 0.34; 'skip:k 20': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'add': 0.36; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'from:no real name:2**0': 0.60; 'subject:there': 0.65; 'ethan,': 0.84; 'furman': 0.84; 'subject:before': 0.84; 'ethan': 0.91 Newsgroups: comp.lang.python Date: Fri, 16 Nov 2012 07:00:37 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.49.62.250; posting-account=cxDMTgoAAAD0NMd7UaWriutT-PIoI910 References: <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 173.49.62.250 MIME-Version: 1.0 Subject: Re: Is there a simpler way to modify all arguments in a function before using the arguments? From: bruceg113355@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: , Message-ID: Lines: 97 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353078041 news.xs4all.nl 6935 [2001:888:2000:d::a6]:43769 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33443 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. However, this works: from functools import wraps def fix_args(fn): @wraps(fn) def wrapper(*args, **kwargs): args = (arg.replace('_', '') for arg in args) for kv in kwargs: kwargs[kv] = kwargs[kv].replace('_', '') return fn(*args, **kwargs) return wrapper @fix_args def foo(a1="", a2="", b1="", b2=""): print(a1) print(a2) print(b1) print(b2) print "" foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x') foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x') foo ('a1a1_x', 'a2a2_x', b1='b1b1_x', b2='b2b2_____x') Bruce