Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18722
| References | <e1a94d90-57b4-4f6c-be9a-bef08f978639@h13g2000vbn.googlegroups.com> |
|---|---|
| Date | 2012-01-09 15:58 -0800 |
| Subject | Re: Explanation about for |
| From | Chris Rebert <clp2@rebertia.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4561.1326153525.27778.python-list@python.org> (permalink) |
On Mon, Jan 9, 2012 at 3:23 PM, Νικόλαος Κούρας <nikos.kouras@gmail.com> wrote:
> ================================
> dataset = cursor.fetchall()
>
> for row in dataset:
> print ( "<tr>" )
>
> for item in row:
> print ( "<td><b><font color=yellow> %s </td>" % item )
> ================================
>
> and this:
>
Your second snippet makes use of Python's
iterable/sequence/tuple-unpacking feature. Here's the relevant part of
the Language Reference:
http://docs.python.org/reference/simple_stmts.html#assignment-statements
By way of example, given:
seq = [1,2,3,4]
Then:
w, x, y, z = seq
Results in:
w = 1
x = 2
y = 3
z = 4
`seq` has been "unpacked", and its elements have been assigned to
variables (namely: `w`, `x`, `y`, and `z`). If the number of variables
doesn't match the number of elements, Python will raise an exception.
> ================================
> dataset = cursor.fetchall()
>
> for host, hits, agent, date in dataset:
for-loops perform repeated assignments to the loop variable(s), and,
like with the simple assignment statement example I gave, also permit
the use of unpacking in such assignments. To make the unpacking more
explicit, the loop can be equivalently rewritten as:
for _row in dataset:
host, hits, agent, date = _row
# rest same as before...
> print ( "<tr>" )
>
> for item in host, hits, agent, date:
Python's syntax for tuples is based solely on commas and does not
require parentheses, though a tuple's repr() always includes the
parentheses and programmers often/typically do too. (See
http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences
.) For example:
x = 1, 2
And:
x = (1, 2)
Both do exactly the same thing: set `x` to a tuple of length 2
containing the elements 1 and 2.
The loop thus might be more clearly written as:
for item in (host, hits, agent, date):
So, what's happening is that Python is iterating over the elements of
an anonymous literal tuple; `item` will thus take on the values of
`host`, `hits`, `agent`, and `date`, in turn.
> print ( "<td><b><font color=white> %s </td>" % item )
> ================================
Cheers,
Chris
--
http://rebertia.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-09 15:23 -0800
Re: Explanation about for Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-09 16:42 -0700
Re: Explanation about for Chris Rebert <clp2@rebertia.com> - 2012-01-09 15:58 -0800
Re: Explanation about for Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-09 18:11 -0700
Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 01:02 -0800
Re: Explanation about for Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-01-10 11:57 +0100
Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 03:38 -0800
Re: Explanation about for Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-01-10 14:52 +0200
Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 03:37 -0800
Re: Explanation about for "Frank Millman" <frank@chagford.com> - 2012-01-10 15:39 +0200
Re: Explanation about for Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-01-10 16:22 +0100
Re: Explanation about for RainyDay <andrei.avk@gmail.com> - 2012-01-11 14:39 -0800
Re: Explanation about for Nick Dokos <nicholas.dokos@hp.com> - 2012-01-10 10:24 -0500
Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-11 14:50 -0800
csiph-web