title: Simulating a static variable for a function
body: <p><strong>SOLUTION</strong>: The error was that my function is actually a method.</p>
  
  <p>I'm trying to use a decorator to simulate a variable for a function. Why do I get this error on the last line below? <code>global name 'onecmd' is not defined</code></p>
  
  <pre><code>def static_var(varname, initial_value):
    def decorate(func):
          setattr(func, varname, initial_value)
          return func
    return decorate
  
  @static_var("last", None)
  def onecmd(self, line):
      if line == "lastcmd":
          line = onecmd.last
      else:
          onecmd.last = line
  </code></pre>