Decorators and python objects demystified

Posted on Thu 05 December 2013 in Notes

This article explains very well how decorators works, and a lot about how everything in Python is an object (last time I'll be making a variable with the same name as a function!)

http://yasoob.github.io/blog/python-decorators-demystified/

The jist is that any function is an object and can be parsed into another function, and the @-decorator is shorthand for doing just that!

def func_a(func):
    print "func a"
    func()
def func_b():
    print "func b"

func_b = func_a(func_b)
func_b()
# func a
# func b

is the same as

def func_a(func):
    print "func a"
    func()
@func_a
def func_b():
    print "func b"

func_b()
# func a
# func b