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


Groups > comp.lang.python > #40185

Re: suggestions for improving code fragment please

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 <msirenef@lightbird.net>
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 <msirenef@lightbird.net>
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 <u8idnaoktKrcKbLMnZ2dnUVZ8uednZ2d@brightview.co.uk>
In-Reply-To <u8idnaoktKrcKbLMnZ2dnUVZ8uednZ2d@brightview.co.uk>
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 <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.2683.1362086549.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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


Thread

suggestions for improving code fragment please The Night Tripper <jkn+gg@nicorp.co.uk> - 2013-02-28 19:47 +0000
  Re: suggestions for improving code fragment please Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-28 11:55 -0800
  Re: suggestions for improving code fragment please Joel Goldstick <joel.goldstick@gmail.com> - 2013-02-28 14:56 -0500
    Re: suggestions for improving code fragment please The Night Tripper <jkn+gg@nicorp.co.uk> - 2013-02-28 23:20 +0000
  Re: suggestions for improving code fragment please Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-28 12:58 -0700
  Re: suggestions for improving code fragment please Tim Chase <python.list@tim.thechases.com> - 2013-02-28 14:37 -0600
  Re: suggestions for improving code fragment please MRAB <python@mrabarnett.plus.com> - 2013-02-28 21:13 +0000
  Re: suggestions for improving code fragment please Mitya Sirenef <msirenef@lightbird.net> - 2013-02-28 16:22 -0500
  Re: suggestions for improving code fragment please Dave Angel <davea@davea.name> - 2013-02-28 16:28 -0500
  Re: suggestions for improving code fragment please Terry Reedy <tjreedy@udel.edu> - 2013-02-28 16:30 -0500
  Re: suggestions for improving code fragment please Tim Chase <python.list@tim.thechases.com> - 2013-02-28 16:44 -0600
  Re: suggestions for improving code fragment please Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-01 02:44 +0000

csiph-web