How to pass a function with a argument in a basic decorator using Python?
<p>I am reading about decorators and have learned quite a bit from <a href="http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/" rel="nofollow">Simeons blog</a> I had success before when there was no arguments passed in the function like this: decorator(func). I think my issue is decorator(func(args)). I think my issue is func(args) is executing and passing its return value to the decorator instead of the function itself. How can I pass a function &amp; argument in a decorator (example B) and not use the @decorator_name 'decoration' style (example A).</p>

<p>Edit: I would like example B to produce the same result as example A.</p>

<pre><code>user_input = input("Type what you want:  ")

def camel_case(func):
   def _decorator(arg):
      s = ""
      words = arg.split()
      word_count = len(words)
      for i in range(word_count):
         if i &gt;0:
            s += words[i].title()
         else:
            s += words[i].lower()
      return func(s)
   return _decorator
</code></pre>

<p>Example A: This works</p>

<pre><code>@camel_case
def read(text):
   print("\n" + text" )

read(user_input)
</code></pre>

<p>Example B: This does not work</p>

<pre><code>def read(text):
   print("\n" + text" )

camel_case(read(user_input))
</code></pre>

<p>THe decorator takes a function:  <code>camel_case(read)</code>.  If you're trying to apply <code>camel_case</code> to the <code>read(user_input)</code> function, try this:</p>

<pre><code>camel_case(read)(user_input)
</code></pre>

<p><code>camel_case(read)</code> returns the decorated function, which you then call with <code>(user_input)</code>.</p>

python python-decorators