Explanation about for

Nick Dokos nicholas.dokos at hp.com
Tue Jan 10 10:24:10 EST 2012


Νικόλαος Κούρας <nikos.kouras at gmail.com> wrote:

> On 10 Ιαν, 03:11, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> > 2012/1/9 Íéêüëáïò Êïýñáò <nikos.kou... at gmail.com>:
> >
> > > if the MySQL query was:
> >
> > > cursor.execute( '''SELECT host, hits, agent, date FROM visitors WHERE pin =
> > > %s ORDER BY date DESC''', pin )
> >
> > > can you help me imagine how the mysql database cursor that holds the query
> > > results would look like? I must somehow visualize it in order to understand
> > > it!
> >
> > You can think of it as a pointer, traversing over one row of the
> > result set at a time.  Hopefully this will come out legibly:
> >
> > -----------------------------------------------
> > | HOST | HITS | AGENT | DATE |
> > -----------------------------------------------            -------------
> > | foo     | 7       | IE6       | 1/1/11 |   <----   | cursor |
> > -----------------------------------------------            -------------
> > | bar     | 42     | Firefox  | 2/2/10 |
> > -----------------------------------------------
> > | baz    | 4       | Chrome | 3/3/09 |
> > ------------------------------------------------
> 
> Database cursor is the pointer that iterates over the result set one
> row at a time?
> I though that it was the name we give to the whole mysql result set
> returned my cursor.execute.
> 
> >
> >
> > > Also what happend if the query was:
> > > cursor.execute( '''SELECT host FROM visitors") ?
> >
> > > the result would have to be something likelike?
> >
> > > -----------------
> > > |somehost1|
> > > -----------------
> > > |somehost2|
> > > -----------------
> > > |somehost3|
> > > -----------------
> > > .....................
> > > .....................
> > > |somehost n|
> > > -----------------
> >
> > > So what values host, hits, agent, date would have in 'for host, hits, agent,
> > > date in
> > >  dataset' ? Every row has one string how can that be split in 4?
> >
> > Why don't you try it and see what happens?  But to spare you the
> > suspense, you would get:
> >
> > ValueError: need more than 1 value to unpack
> >
> > Because you can't unpack a 1-length tuple into four variables.  The
> > code assumes that the query is selecting exactly 4 columns.
> 
> 
> -----------------------------------------------
> | HOST    | HITS    | AGENT     | DATE |
> -----------------------------------------------
> | foo     | 7       | IE6       | 1/1/11 |
> -----------------------------------------------
> | bar     | 42      | Firefox   | 2/2/10 |
> -----------------------------------------------
> | baz     | 4       | Chrome    | 3/3/09 |
> -----------------------------------------------
> 
> In this line:
> for host, hits, agent, date in dataset:
> 
> 'dataset' is one of the rows of the mysql result or the whole mysql
> result set like the table above?
> 
> I still have trouble understanding this line :(

You can think of it as a list of tuples. Forget about cursors and
databases for now. If l is a list [1, 2, 3, 4] you iterate over it like
this:

for x in l:
  print x

and you get each element of the list[fn:1]. Similarly if l is a list of tuples
l = [(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)] you can iterate over the
list:

for x in l:
  print x

In this case, x is going to be (1,2,3,4) the first time through the loop, (5,6,7,8)
the second time and so on. You can break each x apart within the loop:

for x in l:
  t1, t2, t3, t4 = x
  print x, t1, t2, t3, t4

or you can break it apart like this - it's essentially the same thing:

for t1, t2, t3, t4 in l:
  print t1, t2, t3, t4

You have been encouraged repeatedly to try these things interactively:
please do so with the above examples and all will become clear.

Going back to cursors and databases: you *can* think of 'dataset' as
being a list of tuples - a list of all the query results, but with one
proviso. The difference when you use a cursor is that `dataset' may
be a lazy list (an "iterator"): instead of the whole set of results
being in memory at the same time, the system will take care of doing
whatever is necessary to get more of the results when it needs them. But
the behavior is the *same* in the sense that the output that you get is
the same (you will only see differences if you are keeping an eye on how
much memory and how much time your program is using).

Nick

Footnotes:

[fn:1] ... and, no, you *can't express* this as
"the first time it is

for x in 1:
  ...

and the second time it is

for x in 2:
  ...

" as you asked in another email. That's arrant nonsense: x takes
successive values in the list l for every iteration of the for
loop. This is elementary Python (nay, elementary programming, period).



More information about the Python-list mailing list