Why doesn't warp function in Python get executed when I call it?
<p>I have this simple code.</p>

<pre><code>def decor(func):
  def wrap():
    print("============")
    func()
    print("============")
  return wrap

def print_text():
  print("Hello world!")

decor(print_text())
</code></pre>

<p>Why does this only print "Hello world!" and not the two wrappers? </p>

<p>That is because you only return the function wrap - It is not called. If you do call it, either by assigning the result to a variable, or by using <code>decor(print_text)()</code> directly. Note that you should use <code>print_text</code> and not <code>print_text()</code>, as the latter will give the result of the function to decor, rather than the function itself. The working version would be:</p>

<pre><code>def decor(func):
  def wrap():
    print("============")
    func()
    print("============")
  return wrap

def print_text():
  print "Hello world!"

wrapped_function = decor(print_text)
wrapped_function()
</code></pre>

python wrap