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>You are calling <code>decor</code> with a single argument.  That argument is whatever <code>print_text()</code> returns.  Well, <code>print_text()</code> prints something and then returns None.  So far, your output is just <code>Hello world!</code>.  None is now passed to <code>decor</code>, and it returns the <code>wrap()</code> function.  Nowhere is <code>wrap()</code> actually called, and your final output is just <code>Hello world!</code>.</p>

<p>You are misunderstanding the use of decorators. The correct syntax is this:</p>

<pre><code>@decor
def print_text():
    print("Hello world!")
</code></pre>

<p>That is a shortcut for this:</p>

<pre><code>def print_text():
    print("Hello world!")

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

<p>Now you can see that it is the returns of <code>decor(print_text)</code> (the <code>wrap()</code> function) that is being called, and everything is printed.</p>

python wrap