Non-obvious name bindings

Don Garrett garrett at bgb.cc
Tue Nov 13 19:31:14 EST 2001


  Actually, I would argue that the 'e' in the for loop should be 
temporary as well. I might also argue that these temporary variables 
shouldn't be assignable inside the body of the loop.

  However, I don't personally consider this a major issue, just
a matter of personnal preference.

Emile van Sebille wrote:
> 
> ----- 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
> 
> ---------

--
Don Garrett                     http://www.bgb-consulting.com/garrett/
BGB Consulting                                        dgarrett at acm.org



More information about the Python-list mailing list