title: What is the benefit to define a function in a function in python?
body: <p>I encountered this piece of python code (pasted below) on <a href="http://effbot.org/zone/re-sub.htm#unescape-html">effbot</a> and I was wondering:</p>
  
  <p><strong>Why defining a function within a function?</strong></p>
  
  <pre><code>import re, htmlentitydefs
  
  ##
  # Removes HTML or XML character references and entities from a text string.
  #
  # @param text The HTML (or XML) source text.
  # @return The plain text, as a Unicode string, if necessary.
  
  def unescape(text):
      def fixup(m):
          text = m.group(0)
          if text[:2] == "&amp;#":
              # character reference
              try:
                  if text[:3] == "&amp;#x":
                      return unichr(int(text[3:-1], 16))
                  else:
                      return unichr(int(text[2:-1]))
              except ValueError:
                  pass
          else:
              # named entity
              try:
                  text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
              except KeyError:
                  pass
          return text # leave as is
      return re.sub("(?s)&lt;[^&gt;]*&gt;|&amp;#?\w+;", fixup, text)
  </code></pre>