Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decorators
#1
I was going through a tutorial and encountered this code. Can anybody please make me understand the control flow of this code? Like which line is executed first and what is being passed as argument. I apologize if it is silly but i am mid newbie.

I know that when control encounters @decorator_function, it treat it as the beneath function being passed as argument to the decorator function but then it means that
original_function=display
or
original_function=display(): print('display function ran') 
or what else.
And also when wrapper function is returned, than does that mean or can we say that display defination is replaced by wrapper function

@decorator_function
def display():
    wrapper_function
OR

@decorator_function
wrapper_function
Please explain...

def decorator_function(original_function):
    def wrapper_function():
        print('wrapper executed this before {}'.format(original_function.__name__))
        return original_function()
    return wrapper_function

@decorator_function
def display():
    print('display function ran')

#display()

if __name__ == '__main__':
    display()
Output:
wrapper executed this before display display function ran
I understand that the above python code and the below code both are same. But need to understand the proper control flow.
def decorator_function(original_function):
    def wrapper_function():
        print('wrapper executed this before {}'.format(original_function.__name__))
        return original_function()
    return wrapper_function

#@decorator_function
def display():
    print('display function ran')

#display()

if __name__ == '__main__':
    display()
    dd=decorator_function(display)
    dd()
Reply
#2
The only thing to understand is that
@spam
def eggs():
    print('ham')
is equivalent to
def eggs():
    print('ham')
eggs = spam(eggs)
Reply
#3
(Aug-11-2018, 10:00 PM)Gribouillis Wrote: The only thing to understand is that
@spam
def eggs():
    print('ham')
is equivalent to
def eggs():
    print('ham')
eggs = spam(eggs)

Okay thanks Sir. I get it .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decorators @ annotation drcl 3 395 Feb-24-2024, 06:12 AM
Last Post: Gribouillis
  Does @ at sign used for tother than decorators? ggpython000 1 542 Oct-12-2023, 09:08 AM
Last Post: buran
  Variable Scopes Decorators oclmedyb 6 2,738 Jan-15-2021, 02:13 PM
Last Post: oclmedyb
  Decorators and return statements JonEdward 2 1,891 Jul-24-2020, 05:02 PM
Last Post: JonEdward
  How to print cache from Decorators with Memoization OlgaM 2 2,069 Jan-29-2020, 05:06 PM
Last Post: OlgaM
  Python decorators. dodiko1d 0 8,271 Oct-13-2019, 10:23 AM
Last Post: dodiko1d
  learning decorators Prince_Bhatia 6 3,309 Aug-13-2018, 02:28 PM
Last Post: Prince_Bhatia
  decorators within decorators mp3909 6 4,248 Apr-02-2018, 09:47 AM
Last Post: hf8bm
  decorators mp3909 9 5,597 Mar-25-2018, 05:28 PM
Last Post: mp3909

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020