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


Groups > comp.lang.python > #18722

Re: Explanation about for

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'reference:': 0.07; 'snippet': 0.07; 'python': 0.08; 'exception.': 0.09; 'host,': 0.09; 'rewritten': 0.09; 'tuple': 0.09; 'anonymous': 0.15; '(1,': 0.16; '(host,': 0.16; '..)': 0.16; 'assignments.': 0.16; 'elements,': 0.16; 'given:': 0.16; 'parentheses': 0.16; 'repr()': 0.16; 'row': 0.16; 'then:': 0.16; 'tuple;': 0.16; 'unpacking': 0.16; 'url:datastructures': 0.16; 'cc:addr:python-list': 0.16; 'mon,': 0.16; 'syntax': 0.16; 'this:': 0.16; 'language': 0.17; 'wrote:': 0.18; 'repeated': 0.18; 'jan': 0.19; 'cc:no real name:2**0': 0.20; 'written': 0.20; 'cheers,': 0.20; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'received:209.85.212.46': 0.23; 'received:mail- vw0-f46.google.com': 0.23; 'statement': 0.23; 'thus': 0.23; 'feature.': 0.23; 'literal': 0.23; 'cc:2**0': 0.24; 'happening': 0.24; "python's": 0.24; 'raise': 0.28; '(see': 0.28; 'message- id:@mail.gmail.com': 0.28; 'elements': 0.29; 'date:': 0.29; 'cc:addr:python.org': 0.29; 'second': 0.29; 'pm,': 0.29; 'example': 0.29; 'iterating': 0.30; 'tuples': 0.30; 'chris': 0.30; 'does': 0.32; 'values': 0.32; 'in:': 0.32; 'assigned': 0.32; 'programmers': 0.32; 'match': 0.34; 'loop': 0.34; 'received:209.85.212': 0.34; 'assignment': 0.34; 'rest': 0.35; 'url:python': 0.36; 'example,': 0.37; 'variables': 0.37; 'received:google.com': 0.37; 'skip:\xce 10': 0.38; 'received:209.85': 0.38; 'clearly': 0.39; 'url:docs': 0.39; 'url:org': 0.39; 'received:209': 0.40; 'might': 0.40; 'more': 0.61; 'your': 0.61; 'results': 0.63; '2012': 0.67; 'skip:= 30': 0.68; '8bit%:100': 0.69; 'relevant': 0.70; 'as:': 0.70; 'and:': 0.84; 'before...': 0.84; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84; 'url:reference': 0.84; 'agent,': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=8PnWAZtTCW69BH9Ry2IoqeFlvDHRacdcX3g4+8xLCfQ=; b=Dgx8KerxiFOTZ/1aYQHF3a+7likYQkRegoDRLZAwIu+0CEWGt8UB8qN5Jp9InZrmXw mO0iD8kD7PZGfzXplJCqDnA4b9kvs2/aRCVZFZUlNNbkOQck6JMnLYbg0WUDb0EWKhnh pBa5srkLCbYCRbDjRz1jZPwByem59vzTtv4Q0=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <e1a94d90-57b4-4f6c-be9a-bef08f978639@h13g2000vbn.googlegroups.com>
References <e1a94d90-57b4-4f6c-be9a-bef08f978639@h13g2000vbn.googlegroups.com>
Date Mon, 9 Jan 2012 15:58:41 -0800
X-Google-Sender-Auth diCp7F3qQ3-UDGjuonV1ub8-8gw
Subject Re: Explanation about for
From Chris Rebert <clp2@rebertia.com>
To Νικόλαος Κούρας <nikos.kouras@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
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.4561.1326153525.27778.python-list@python.org> (permalink)
Lines 79
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326153525 news.xs4all.nl 6854 [2001:888:2000:d::a6]:48398
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18722

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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