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


Groups > comp.lang.python > #20778

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
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; 'fixes': 0.05; 'one?': 0.05; 'subject:when': 0.07; 'python': 0.08; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'minus': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'run.': 0.09; 'willmer': 0.09; 'wrong,': 0.09; '~ethan~': 0.09; 'construct.': 0.16; 'need:': 0.16; 'received:50': 0.16; 'runs.': 0.16; 'scope,': 0.16; 'take?': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'seems': 0.20; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'performing': 0.26; 'stuff': 0.26; 'code': 0.26; 'variable': 0.28; 'raise': 0.28; 'needed,': 0.28; 'avoiding': 0.29; 'problem': 0.29; 'print': 0.29; 'alex': 0.31; 'reporting': 0.31; 'thu,': 0.32; "i've": 0.32; 'header:User- Agent:1': 0.33; 'instead': 0.33; 'loop': 0.34; 'steven': 0.34; 'record': 0.34; 'subject:/': 0.34; 'surprised': 0.34; 'to:addr :python-list': 0.35; 'before.': 0.37; 'variables': 0.37; 'but': 0.37; 'should': 0.38; 'sometimes': 0.38; 'e.g.': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'choose': 0.64; 'received:websitewelcome.com': 0.64; 'care': 0.71; 'guaranteed': 0.77; 'general:': 0.84; 'hand.': 0.84; 'refuses': 0.84
Date Thu, 23 Feb 2012 19:49:01 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Thunderbird 2.0.0.24 (Windows/20100228)
MIME-Version 1.0
To python-list@python.org
Subject Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty
References <14ba4b39-4066-4d24-89d7-3c7c85aab85c@b18g2000vbz.googlegroups.com> <4f46e329$0$29986$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <4f46e329$0$29986$c3e8da3$5496439d@news.astraweb.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 no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender c-50-137-149-57.hsd1.or.comcast.net ([192.168.74.10]) [50.137.149.57]:3728
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.107.1330059073.3037.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330059073 news.xs4all.nl 6852 [2001:888:2000:d::a6]:48750
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20778

Show key headers only | View raw


Steven D'Aprano wrote:
> On Thu, 23 Feb 2012 16:30:09 -0800, Alex Willmer wrote:
> 
>> This week I was slightly surprised by a behaviour that I've not
>> considered before. I've long used
>>
>> for i, x in enumerate(seq):
>>    # do stuff
>>
>> as a standard looping-with-index construct. In Python for loops don't
>> create a scope, so the loop variables are available afterward. I've
>> sometimes used this to print or return a record count e.g.
>>
>> for i, x in enumerate(seq):
>>    # do stuff
>> print 'Processed %i records' % i+1
>>
>> However as I found out, if seq is empty then i and x are never created.
> 
> This has nothing to do with enumerate. It applies to for loops in 
> general: the loop variable is not initialised if the loop never runs. 
> What value should it take? Zero? Minus one? The empty string? None? 
> Whatever answer Python choose would be almost always wrong, so it refuses 
> to guess.
> 
> 
>> The above code will raise NameError. So if a record count is needed, and
>> the loop is not guaranteed to execute the following seems more correct:
>>
>> i = 0
>> for x in seq:
>>     # do stuff
>>     i += 1
>> print 'Processed %i records' % i
> 
> What fixes the problem is not avoiding enumerate, or performing the 
> increments in slow Python instead of fast C, but that you initialise the 
> loop variable you care about before the loop in case it doesn't run.
> 
> i = 0
> for i,x in enumerate(seq):
>     # do stuff
> 
> is all you need: the addition of one extra line, to initialise the loop 
> variable i (and, if you need it, x) before hand.

Actually,

i = -1

or his reporting will be wrong.

~Ethan~

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


Thread

A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Alex Willmer <alex@moreati.org.uk> - 2012-02-23 16:30 -0800
  Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-24 01:08 +0000
    Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Ethan Furman <ethan@stoneleaf.us> - 2012-02-23 19:49 -0800
    Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-24 07:10 +0000
    Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Peter Otten <__peter__@web.de> - 2012-02-24 09:44 +0100
  Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Paul Rubin <no.email@nospam.invalid> - 2012-02-23 17:21 -0800
  Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-24 04:32 -0800
    Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Peter Otten <__peter__@web.de> - 2012-02-24 13:44 +0100
      Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Peter Otten <__peter__@web.de> - 2012-02-24 14:14 +0100
      Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-24 14:54 +0000
        Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Arnaud Delobelle <arnodel@gmail.com> - 2012-02-24 15:00 +0000
        Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Peter Otten <__peter__@web.de> - 2012-02-24 16:16 +0100
        Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-28 14:56 -0800
          Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Chris Angelico <rosuav@gmail.com> - 2012-02-29 10:24 +1100
            Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-29 00:18 +0000

csiph-web