Non-obvious name bindings

Emile van Sebille emile at fenx.com
Mon Nov 5 14:13:26 CET 2001


----- Original Message -----
From: "Kerim Borchaev" <warkid at storm.ru>
Newsgroups: comp.lang.python
Sent: Monday, November 05, 2001 4:49 AM
Subject: Non-obvious name bindings


> Hello!
>
>   It seemed that it was my failure of understanding python but the
>   fact that introducing variable in list comprehension binds it to
>   functions scope(as every declaration means to?) is not really
>   obvious.
>
>   I mean this:
>
> >>> [e for e in ['exists']]
> ['exists']
> >>> e
> 'exists'
>
>   And I'm still confused - those "temporary" variables declaration
>   looks so "innocent", and in most cases (in every case for list
>   comprehensions I guess?) it's everyones intension not to use this
>   variable somewhere outside the loop.
>
>   What are your thoughts?

If you look at the structure as shorthand for a for loop and list.append it
may make more sense:

>>> [e for e in 'exists']
['e', 'x', 'i', 's', 't', 's']
>>> e
's'

>>> for e in 'exists':
 print e,
e x i s t s
>>> e
's'

HTH,

Emile van Sebille
emile at fenx.com

---------





More information about the Python-list mailing list