I want to ask Python experts about function decorator which has arguments.

I feel that function decorator having arguments is complicated,
because three 'def' are nested:

  def multiply(n):
    def deco(func):
      def newfunc(*args, **kwargs):
        return n * func(*args, **kwargs)
      return newfunc
    return deco

  @multiply(4)
  def f1(x, y):
    return x+y

  print(f1(2, 3))   #=> 20  (= 4 * (2+3))


If function decorator notation could take arguments,
decorator definition would be more simple:

  def multiply(func, n):
    def newfunc(*args, **kwargs):
      return n * func(*args, **kwargs)
    return newfunc

  @multiply 4      # ex: @decorator arg1, arg2, arg3
  def f1(x, y):
    return x+y


How do you think about this idea?

--
regards,
makoto kuwata
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to