Multiple distinct objects: Difference between revisions
Content added Content deleted
(added python) |
|||
Line 33: | Line 33: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
The mistake is often written as: |
The mistake is often written as: |
||
<code python>[Foo()] * n # here Foo() can be any expression that returns |
<code python>[Foo()] * n # here Foo() can be any expression that returns a new object</code> |
||
which is incorrect since <code>Foo()</code> is only evaluated once. A common correct version is: |
which is incorrect since <code>Foo()</code> is only evaluated once. A common correct version is: |
||
<code python>[Foo() for i in xrange(n)]</code> |
<code python>[Foo() for i in xrange(n)]</code> |