Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20783
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty |
| Date | 2012-02-24 07:10 +0000 |
| References | <14ba4b39-4066-4d24-89d7-3c7c85aab85c@b18g2000vbz.googlegroups.com> <4f46e329$0$29986$c3e8da3$5496439d@news.astraweb.com> <4F4708AD.1020805@stoneleaf.us> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.110.1330067451.3037.python-list@python.org> (permalink) |
On 24/02/2012 03:49, Ethan Furman wrote: > 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~ Methinks an off by one error :) -- Cheers. Mark Lawrence.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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