for loop without variable

Mike Meyer mwm-keyword-python.b4bdba at mired.org
Fri Jan 11 02:19:38 EST 2008


On Fri, 11 Jan 2008 01:48:43 -0500 Marty <martyb1 at earthlink.net> wrote:
> Mike Meyer wrote:
> >> This caused me to wonder why Python does not have a "foreach" statement (and 
> >> also why has it not come up in this thread)?  I realize the topic has probably 
> >> been beaten to death in earlier thread(s), but does anyone have the short answer?
> > 
> > But I'm curious - what's the difference between the "foreach" you have
> > in mind and the standard python "for"?
> For example, I thought the python "equivalent" of perl's foreach might be:


No, python's equivalent of Perl's foreach is "for". I.e.

    foreach $var (@list)

does the same thing as Python's

    for var in list

(except Perl gets the scoping right).

Maybe you're thinking of Perls "default variable" feature (I don't
know what else to call it), which implicitly uses $_ as a variable in
any number of places if you fail to provide a variable? So that you
can say:

   foreach (@list)

and apparently not have to use a variable,  except it implicitly uses $_.

    <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.



More information about the Python-list mailing list