Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to learn about python function decorator. So far I am following a tutorial. I have writing the same code. but still I am getting the error that
NameError: name 'new_decorator' is not defined
. What could be the reason for that. Thanks a lot.

What I have tried:

@new_decorator
def func_needs_decorator():
    print("This function is in need of a Decorator")


def new_decorator(func):

    def wrap_func():
        print("Code would be here, before executing the func")

        func()

        print("Code here will execute after the func()")

    return wrap_func

func_needs_decorator()
Posted
Updated 23-Apr-20 3:08am

1 solution

Python is interpreted language and thus each type needs to be defined before it can be used in the program ahead. Try defining the new_decorator above your func_needs_decorator function.
Python
def new_decorator(func):
    def wrap_func():
        print("Code would be here, before executing the func")
        func()
        print("Code here will execute after the func()")
    return wrap_func

@new_decorator
def func_needs_decorator():
    print("This function is in need of a Decorator")


func_needs_decorator()
Check out more here: https://www.datacamp.com/community/tutorials/decorators-python[^]
 
Share this answer
 
v2
Comments
hamid18 23-Apr-20 9:13am    
thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900