user7696297
user7696297

Reputation:

What is a function decorator in python

What is a function decorator? Is it something we use to declare a function? Or is it like a constructor.

Upvotes: 1

Views: 660

Answers (1)

chepner
chepner

Reputation: 531798

A function decorator is simply a function intended to take a function as an argument and return a new function to use in its place. Python provides decorator syntax to simply its use. That is,

@foo
def bar():
    pass

is equivalent to

def bar():
    pass
bar = foo(bar)

The syntax takes care of applying the decorator to the original function and rebinding the result to the original name.

Upvotes: 1

Related Questions