What is wrapper.count += 1?

Screen Link: https://app.dataquest.io/m/414/decorators%3A-advanced/1/introduction

My Code:

def counter(func):
    def wrapper(*args, **kwargs):
        wrapper.count += 1
        # Call the function being decorated and return the result
        return func(*args,**kwargs)

    wrapper.count = 0
    # Return the new decorated function
    return wrapper
@counter
def foo():
    print('calling foo()')    

foo()
foo()

print('foo() was called {} times.'.format(foo.count))

Could you tell me what wrapper.count is in the wrapper function? What is a concept behind this?

This program counts how many times each function is called.

Thank you.

5 Likes

This is call function attributes. please check out these references

3 Likes

Thank you so much for your answer! I will look it up!

1 Like

Thank you for the reference material; however, I’m not sure that understand why we were given an exercise to complete when the items you referenced was not covered in the course materials. One of the reasons, I subscribe to Dataquest is because of the great explanations. This is very confusing.

2 Likes

Hi @adamson.tracy,

Could you please share your feedback (about the materials that aren’t covered in the course) with the Content & Product teams of Dataquest? Just click the ? button in the upper-right corner of any screen of the Dataquest learning platform, select Share Feedback, fill in the form, and send it. Thanks!

2 Likes

Thank you for the response. I will do that moving forward.

1 Like