Home > Software engineering >  Why decorator is giving missing 1 required positional argument
Why decorator is giving missing 1 required positional argument

Time:09-12

hey developer I am just exploring decorator so there I got some error missing 1 required positional argument. but when I driving code in calling function i am providing particular value first look my code.

def main(func):
    def wrapper(*args, **kwargs):
        r, pi = func()
        return pi * r
    return wrapper


@main
def area(r, pi=3.14):
    return r, pi



#drive
print(area(r=10))

CodePudding user response:

You need to pass the wrapper arguments to func():

r, pi = func(*args, **kwargs)

Without that, it's calling area() with no arguments, when area() expects at least one argument.

CodePudding user response:

You are missing the r argument value in line 3: r, pi = func(): Pass *args, **kwargs in the func.

def main(func):
    def wrapper(*args, **kwargs):
        r, pi = func(*args, **kwargs) ## here
        return pi * r
    return wrapper


@main
def area(r, pi=3.14):
    return r, pi



#drive
print(area(10))

This will fix your issue

output: 31.400000000000002

  • Related