How can I understan the "for" here?

D'Arcy J.M. Cain darcy at druid.net
Thu Dec 11 16:03:12 CET 2008


On Thu, 11 Dec 2008 22:44:20 +0800
Kermit Mei <kermit.mei at gmail.com> wrote:
>  >>> ["%s=%s" % (k, v) for k, v in params.items()]
> ['pwd=secret', 'database=master', 'uid=sa', 'server=mpilgrim']
> 
> I can't understand the second sentence because of the "for ... in".
> I consider that the syntactics of "for" should be:
> 
> for k,v in params.items():

I think you are binding ':' here too tightly to the 'for' construct in
your mind.  The for construct is "for <variable(s)> in <sequence>"
which can be used as <block construct>:<statement(s)> in block
constructs.  The above is not a block construct but a list
comprehension so it follows different rules.

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list