Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none,': 0.05; 'none:': 0.05; 'subject:code': 0.07; 'val': 0.07; '"""get': 0.09; 'convenience': 0.09; 'function:': 0.09; 'python:': 0.09; 'def': 0.10; 'index': 0.13; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'wrote:': 0.17; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'setting': 0.26; 'values': 0.26; 'subject:please': 0.27; 'index,': 0.29; "i'm": 0.29; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'pm,': 0.35; 'there': 0.35; 'except': 0.36; 'being': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'little': 0.39; 'received:192.168': 0.40; 'night': 0.62; 'frank': 0.75; 'dumb': 0.84; 'time.\xe2\x80\x9d': 0.84; 'do:': 0.91 Date: Thu, 28 Feb 2013 16:22:24 -0500 From: Mitya Sirenef User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: suggestions for improving code fragment please References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362086549 news.xs4all.nl 6889 [2001:888:2000:d::a6]:42158 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40185 On 02/28/2013 02:47 PM, The Night Tripper wrote: > Hi there > I'm being very dumb ... how can I simplify this fragment? > > > if arglist: > arglist.pop(0) > if arglist: > self.myparm1 = arglist.pop(0) > if arglist: > self.myparm2 = arglist.pop(0) > if arglist: > self.myparm3 = arglist.pop(0) > if arglist: > self.parm4 = arglist.pop(0) > # ... > > Thanks > J^n > > I often use this convenience function: def getitem(seq, index, default=None): """Get item from an `seq` at `index`, return default if index out of range.""" try : return seq[index] except IndexError : return default If you're ok with setting myparm values to default None, you can do: self.myparm1, self.myparm2, self.myparm3, self.myparm4 = \ (getitem(arglist, n) for n in range(4)) If you only want to set them when they are in arglist: for n in range(4): val = getitem(arglist, n) if val is not None: setattr(self, "myparm%d" % (n+1), val) -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ “So many books, so little time.” ― Frank Zappa