How can I understan the "for" here?

Kermit Mei kermit.mei at gmail.com
Thu Dec 11 13:03:46 EST 2008


D'Arcy J.M. Cain wrote:
> 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.
>
>   
Yes, I see. Thanks you all.





More information about the Python-list mailing list