Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Function Decorators in Python Set 1 Introduction
    python

    Function Decorators in Python Set 1 Introduction

    March 23, 2023No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Python’s Function Decorators, Part 1 of a Set (Introduction) Read and Discuss Topics Presented in Courses and Videos Background

    The following is a list of essential information about Python functions that is necessary to comprehend decorator.

    functions.

    1. In Python, we can define a function inside another function.
    2. In Python, a function can be passed as parameter to another function (a function can also return another function).
    • Python

    Python


    # A Python program to demonstrate that a function

    # can be defined inside another function and a

    # function can be passed as parameter.

    # Adds a welcome message to the string

    def


    messageWithWelcome(


    str


    ):



    # Nested function



    def


    addWelcome():



    return


    "Welcome to "



    # Return concatenation of addWelcome()



    # and str.



    return


    addWelcome()


    +


    str

    # To get site name to which welcome is added

    def


    site(site_name):



    return


    site_name

    print


    messageWithWelcome(site("GeeksforGeeks"))

    Output:

    Welcome to GeeksforGeeks

    Decorator Function for Functions A function known as a decorator is one that only accepts another function as its input argument and then returns another function. This makes it easier to “wrap” functionality in the same code on several occasions. For instance, the code that was just shown may be rewritten as follows: To define a decorator that will be applied to another function, we use the @func name notation.

    function.

    • Python

    Python


    # Adds a welcome message to the string

    # returned by fun(). Takes fun() as

    # parameter and returns welcome().

    def


    decorate_message(fun):



    # Nested function



    def


    addWelcome(site_name):



    return


    "Welcome to "


    +


    fun(site_name)



    # Decorator returns a function



    return


    addWelcome

    @decorate_message

    def


    site(site_name):



    return


    site_name;

    # Driver code

    # This call is equivalent to call to

    # decorate_message() with function

    # site("GeeksforGeeks") as parameter

    print


    site("GeeksforGeeks")

    Output:

    Welcome to GeeksforGeeks

    In addition, decorators might be helpful when attaching data to something (or adding an attribute to).

    functions.

    • Python

    Python


    # A Python example to demonstrate that

    # decorators can be useful attach data

    # A decorator function to attach

    # data to func

    def


    attach_data(func):



    func.data


    =


    3



    return


    func

    @attach_data

    def


    add (x, y):



    return


    x


    +


    y

    # Driver code

    # This call is equivalent to attach_data()

    # with add() as parameter

    print


    (add(


    2


    ,


    3


    ))

    print


    (add.data)

    Output:

    5
    3

    Calling add(2, 3) would simply yield the sum of two integers, but when we use add.data then ‘add’ function is sent into then decorator function ‘attach data’ as argument and this function returns ‘add’ function with an attribute ‘data’ that is set to 3 and thus displays it. Decorators in Python are a strong tool that may eliminate repetition from code. For more information, please refer to the Decorators in Python documentation.

    Function Decorators in Python Set 1 Introduction Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleOperator Functions in Python Set 1
    Next Article Python Logging A Stroll Through the Source Code

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.