Thursday, August 31, 2017

python function decorators

https://realpython.com/blog/python/primer-on-python-decorators/

https://www.thecodeship.com/patterns/guide-to-python-function-decorators/

https://www.learnpython.org/en/Decorators

decorators dynamically alter the functionality of a function, method, or class

This is ideal when you need to extend the functionality of functions that you don't want to modify.

Things to know for understanding the decorators:

- assign function to a variable:
def f():
    print 'ok'

a = f
a()

- define function inside other function (sub function in a function, and the sub function is called):
- Function as parameter:
def a():
    return 'HELLO'

def b(fun):
    return str(fun()) + 'HI;

b(a)







No comments:

Post a Comment