Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Change the <code>command</code> to:</p> <pre><code>lambda the_year=the_year: year_seter(the_year) </code></pre> <p>The problem has to do with how Python looks up the value of <code>the_year</code>. When you use</p> <pre><code>lambda : year_seter(the_year) </code></pre> <p><code>the_year</code> is not in the local scope of the lambda function, so Python goes looking for it in the <a href="https://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules">extended, global, then builtin scopes</a>. It finds it in the global scope. The <code>for-loop</code> uses <code>the_year</code>, and after the for-loop ends, <code>the_year</code> retains its last value, 2011. Since the lambda function is executed after the for-loop has ended, the value Python assigns to <code>the_year</code> is 2011.</p> <p>In contrast, if you use a parameter with a default value, the default value is fixed at the time the function (lambda) is <strong>defined</strong>. Thus, each <code>lambda</code> gets a different value for <code>the_year</code> fixed as the default value.</p> <p>Now when the lambda is called, again Python goes looking for the value of <code>the_year</code>, but this time finds it in the lambda's local scope. It binds the default value to <code>the_year</code>.</p> <hr> <p>PS. </p> <ol> <li><p>You could also forgo defining <code>year_seter</code> and just do:</p> <pre><code>lambda the_year=the_year: year.set(the_year) </code></pre></li> <li><code>list_of_years = range(1995,2012)</code> also works.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload