简体   繁体   中英

Why decorator is giving missing 1 required positional argument

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))

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM