title: Making decorators with optional arguments
body: <pre><code>from functools import wraps
  
  def foo_register(method_name=None):
      """Does stuff."""
      def decorator(method):
          if method_name is None:
              method.gw_method = method.__name__
          else:
              method.gw_method = method_name
          @wraps(method)
          def wrapper(*args, **kwargs):
              method(*args, **kwargs)
          return wrapper
      return decorator
  </code></pre>
  
  <p>Example: The following decorates <code>my_function</code> with <code>foo_register</code> instead of ever making it to <code>decorator</code>.</p>
  
  <pre><code>@foo_register
  def my_function():
      print('hi...')
  </code></pre>
  
  <p>Example: The following works as expected.</p>
  
  <pre><code>@foo_register('say_hi')
  def my_function():
      print('hi...')
  </code></pre>
  
  <p>If I want it to work correctly in both applications (one using <code>method.__name__</code> and one passing the name in), I have to check inside of <code>foo_register</code> to see if the first argument is a decorator, and if so, I have to: <code>return decorator(method_name)</code> (instead of <code>return decorator</code>). This sort of "check to see if it's a callable" seems very hackish. Is there a nicer way to create a multi-use decorator like this?</p>
  
  <p>P.S. I already know that I can require the decorator to be called, but that's not a "solution". I want the API to feel natural. My wife loves decorating, and I don't want to ruin that.</p>