Gojilla
Gojilla

Reputation: 83

How does this function inside a decorator work in Python?

def title_decorator(print_name_function):
#in addition to printing name it will print a title
    def wrapper():
        #it will wrap print_my_name with some functionality
        print("Professor:")
        print_name_function() #<----- QUESTION HERE
        #wrapper will print out professor and then call print_name_function
    return wrapper

def print_my_name():
    print("Joseph")

def print_mike():
    print("Mike")

decorated_function = title_decorator(print_mike)
#calling decorator function, passing in print_my_name

decorated_function
decorated_function()

Under def wrapper() we have print_name_function(), how does this work and why did we include () here?

I see we are passing in print_name_function into title_decorator

But I don't really understand how it was included in def wrapper() and what it means to include () behind print_name_function()

Upvotes: 0

Views: 45

Answers (2)

Muhammad Anas Atiq
Muhammad Anas Atiq

Reputation: 17

Simply Decorator function is used to enhance the functionality of a function. read the comments of my code hope you understand this.

def title_decorator(print_name_function):   # this function take one function as argument
    def wrapper(): # Here is Rapper function
        print("Professor:")
        print_name_function() #(<----- QUESTION HERE) Answer-----> We are calling the same function because without calling
                                    # doesn't work, basically the function we passed in title_decorator() calls print_name_function()
                                    # inside wrapper function,
        #wrapper will print out professor and then call print_name_function
    return wrapper

def print_my_name():
    print("Joseph")

def print_mike():
    print("Mike")


decorated_function = title_decorator(print_mike)    # Here you calling your function and give print_mike() function as an argument
                        # store it's returning value in variable named as decorated_function, so first time your decorator returns
                        # called wrapper() function
decorated_function()   # and then your variable become decorated_function is working as a function that's why you calling this```


Upvotes: 0

Felipe Borges
Felipe Borges

Reputation: 435

Think in a decorator like a function that return another function. What you want to do is add a behavior to a function without changing his implementation.

In practice that decorator have the following behavior.

def title_decorator(print_name_function):

    # now your decorator have a local variable (print_name_function) 
    # in that function scope that variable is a reference to another 
    # function

    def wrapper():
        # now you write a function that do something

        print("Professor:")
        print_name_function() # here you function is calling a scope 
                              # variable that by the way is another 
                              # function. See that the scope of this 
                              # variable is *title_decorator*


    # now instead to return your argument function you return another
    # with some other behavior
    return wrapper

OBS: When wrapper is calling a function this is a reference to the object. Keep this is mind

Upvotes: 1

Related Questions