Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Remember, in Python, <em>everything is an object and we really mean that</em>. Functions are objects too. We can store functions in a dictionary, and in fact that's exactly what you seem to want to do here.</p> <pre><code>turn_lf = lt(step_size ) </code></pre> <p>The key problem here is that you want to store "a function that calls <code>lt</code> with <code>step_size</code> as the argument", but here you have just called <code>lt</code> with <code>step_size</code> as an argument immediately, and stored the return value.</p> <p>Arguably the simplest way to get what you want is to use <code>functools.partial</code> to "bind" the <code>step_size</code> argument.</p> <pre><code>from functools import partial turn_lf = partial(lt, step_size) # `lt` is not called yet. # Now, later on, we can do turn_lf() # and it's just as if we did `lt(step_size)`. # So now we can store `turn_lf` in a dict, look it up and call it later, etc. # Similarly for all the other functions you want to make. </code></pre> <p>(Another problem is that you haven't been consistent with this; if you want everything to go in one dict, then you need to indicate the bindings for the <code>color</code> function as well. <code>'brown'</code> is just a string, after all. Fortunately, this is just as simple as with the other functions: we just make our <code>partial(color, 'brown')</code>.</p> <p>As for <code>"z" : delete</code>, well - we don't have any arguments to bind to <code>undo</code>. So while we <em>could</em> follow the pattern and write <code>partial(undo)</code> (notice, no more arguments, because we aren't binding anything), it makes much more sense to just write <code>undo</code> directly.</p> <p>As an aside, we can simplify this:</p> <pre><code>for key in key_action: pressed_key = key activated_key = key_action[key] </code></pre> <p>To this:</p> <pre><code>for pressed_key, activated_key in key_action.items(): </code></pre>
 

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