Aug-11-2018, 07:24 PM
(This post was last modified: Aug-11-2018, 07:24 PM by yksingh1097.)
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
or
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
OR
Please explain...
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
1 |
original_function = display |
1 |
original_function = display(): print ( 'display function ran' ) |
And also when wrapper function is returned, than does that mean or can we say that display defination is replaced by wrapper function
1 2 3 |
@decorator_function def display(): wrapper_function |
1 2 |
@decorator_function wrapper_function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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() |