擅长:python、mysql、java
<p>Python本身没有<code>foreach</code>语句。它在语言中内置了<code>for</code>循环。</p>
<pre><code>for element in iterable:
operate(element)
</code></pre>
<p>如果您真的想,可以定义自己的<code>foreach</code>函数:</p>
<pre><code>def foreach(function, iterable):
for element in iterable:
function(element)
</code></pre>
<p>顺便说一下,<code>for element in iterable</code>语法来自于<a href="http://en.wikipedia.org/wiki/ABC_(programming_language)" rel="noreferrer">the ABC</a>编程语言,这是Python的影响之一。</p>