Decorator is a function that takes a function as an argument and returns closure. Closure is a set of inner function and free variable. Inner function is closing over free variable and that is why it is called 'closure'. Free variable is variable that outside the inner function and passed in to inner via docorator.
As the name says, decorator is decorating the received function.
function decorator(undecorated_func):
print("calling decorator func")
inner():
print("I am inside inner")
return undecorated_func
return inner
this is a simple decorator function. It received "undecorated_func" and passed it to inner() as a free variable, inner() printed "I am inside inner" and returned undecorated_func
. When we call decorator(undecorated_func)
, it is returning the inner
. Here is the key, in decorators we are naming the inner function as the name of the function that we passed.
undecorated_function= decorator(undecorated_func)
now inner function is called "undecorated_func". Since inner is now named as "undecorated_func", we passed "undecorated_func" to the decorator and we returned "undecorated_func" plus printed out "I am inside inner". so this print statement decorated our "undecorated_func".
now lets define a class with property decorator:
class Person:
def __init__(self,name):
self._name=name
@property
def name(self):
return self._name
@name.setter
def name(self.value):
self._name=value
when we decorated name() with @property(), this is what happened:
name=property(name) # Person.__dict__ you ll see name
first argument of property() is getter. this is what happened in the second decoration:
name=name.setter(name)
As I mentioned above, decorator returns the inner function, and we name the inner function with the name of the function that we passed.
Here is an important thing to be aware of. "name" is immutable. in the first decoration we got this:
name=property(name)
in the second one we got this
name=name.setter(name)
We are not modifying name obj. In the second decoration, python sees that this is property object and it already had getter. So python creates a new "name" object, adds the "fget" from the first obj and then sets the "fset".