Explanation about for

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Tue Jan 10 05:57:46 EST 2012


Am 10.01.2012 10:02 schrieb Νικόλαος Κούρας:
> -----------------------------------------------
> | 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?

dataset is a cursor, representing the whole result set.

Iterating over it produces one row at each iteration step:

for row in dataset:
     ...

As each row consists of 4 fields, one iteration result is a tuple of 4 
elements.

In this case,

for host, hits, agent, date in dataset:

is shorthand for

for anyunusedvariablename in dataset: # take complete row
     host, hits, agent, date = anyunusedvariablename # tuple unpacking
     del anyunusedvariablename # remove traces

exept that the said variable isn't created.


Thomas



More information about the Python-list mailing list