HomePagev2 Forums Python Discussions what is a Python decorator ?

  • what is a Python decorator ?

     Jia Wu Wu updated 2 years ago 2 Members · 2 Posts
  • Badri Rajasekhar Reddy Rajasekhar Reddy

    Member
    September 10, 2020 at 5:38 pm

    In python decorator is used to modify a function or a class.
    For example:
    def karma(x):

    return x + 1
    atharva = karma
    atharva(10)

    output: 11
    or
    def karma(x):

    return x + 1
    karma =10
    output: 11


    This means that we have two names, i.e. “karma” and “atharva” for the same function.
    The next important fact is that we can delete either “karma” or “atharva” without deleting the function itself.
    del karma
    atharva(10)
    output: 11

  • Jia Wu Wu

    Member
    September 10, 2020 at 6:02 pm

    Is there a typo in the second example? It kind of reassigned ‘karma’ from a function to a numeric variable.

    def karma(x):

    return x + 1
    karma =10
    output: 11

Log in to reply.

Original Post
0 of 0 posts June 2018
Now