A C-like if statement

Paul Rubin http
Fri Feb 24 16:48:06 EST 2006


"Kay Schluehr" <kay.schluehr at gmx.net> writes:
> Hmm. A statement has side-effects but it returns no value. And yes, you
> can create a name within an expression producing a value in Python,
> using a list/generator comprehension. The solution to Bob's problem
> would look like this:
> 
> if (I for I in (a.find("3"),) ) != -1:
>      print "It's here: ", I
> else:
>      print "No 3's here"

I think that works for list comprehensions but not generator
comprehensions.  With generator comprehensions, the index variable's
scope is limited to the comprehension.  With list comprehensions
there's a wart in that the index variable is still around afterwards.



More information about the Python-list mailing list