Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

python decorator TypeError missing 1 required positional argument

I'm trying to write a decorator to repeat an erroring function N times with increasingly sleeping times in between. This is my attempt so far:

def exponential_backoff(seconds=10, attempts=10):
    def our_decorator(func):
        def function_wrapper(*args, **kwargs):
            for s in range(0, seconds*attempts, attempts):
                sleep(s)
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    print(e)
        return function_wrapper
    return our_decorator

@exponential_backoff
def test():    
    for a in range(100):
        if a - random.randint(0,1) == 0:
            print('success count: {}'.format(a))
            pass
        else:
            print('error count {}'.format(a))
            'a' + 1

test()

I keep getting the error:

TypeError: our_decorator() missing 1 required positional argument: 'func'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Understand what decorator is:

@exponential_backoff
def test():
    pass

equals to:

def test():
    pass

test = exponential_backoff(test)

In this case, test is def our_decorator(func):. That's why you get TypeError when calling test().


So further:

@exponential_backoff()
def test():
    pass

equals to:

def test():
    pass

test = exponential_backoff()(test)

In this case, now test is what you need.


Further, functools.wraps helps you to copy all properties of original function to decorated function. Such as function's name or docstring:

from functools import wraps

def exponential_backoff(func):
#   @wraps(func)
    def function_wrapper(*args, **kwargs):
        pass
    return function_wrapper

@exponential_backoff
def test():
    pass

print(test)  # <function exponential_backoff.<locals>.function_wrapper at 0x7fcc343a4268>
# uncomment `@wraps(func)` line:
print(test)  # <function test at 0x7fcc343a4400>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...