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: 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: References: Date: Mon, 9 Jan 2012 15:58:41 -0800 X-Google-Sender-Auth: diCp7F3qQ3-UDGjuonV1ub8-8gw Subject: Re: Explanation about for From: Chris Rebert To: =?UTF-8?B?zp3Ouc66z4zOu86xzr/PgiDOms6/z43Pgc6xz4I=?= 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Mon, Jan 9, 2012 at 3:23 PM, =CE=9D=CE=B9=CE=BA=CF=8C=CE=BB=CE=B1=CE=BF= =CF=82 =CE=9A=CE=BF=CF=8D=CF=81=CE=B1=CF=82 wrote: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > dataset =3D cursor.fetchall() > > for row in dataset: > =C2=A0 =C2=A0print ( "" ) > > =C2=A0 =C2=A0for item in row: > =C2=A0 =C2=A0 =C2=A0 =C2=A0print ( " %s = " % item ) > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > > 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 =3D [1,2,3,4] Then: w, x, y, z =3D seq Results in: w =3D 1 x =3D 2 y =3D 3 z =3D 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. > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > dataset =3D 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 =3D _row # rest same as before... > =C2=A0 =C2=A0print ( "" ) > > =C2=A0 =C2=A0for 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 =3D 1, 2 And: x =3D (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. > =C2=A0 =C2=A0 =C2=A0 =C2=A0print ( " %s "= % item ) > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D Cheers, Chris -- http://rebertia.com