diff options
author | Evan <asyne@users.noreply.github.com> | 2017-02-09 17:57:30 +0200 |
---|---|---|
committer | ven <vendethiel@hotmail.fr> | 2017-02-09 16:57:30 +0100 |
commit | b4ca1f76e2d5b955b011ea8d9b38212634fb1777 (patch) | |
tree | 5a3068374b999d6483b3341b335fb5494d0700e4 /python.html.markdown | |
parent | 1d99d5a7fddb7f05f91686584ec05b88c49f57c3 (diff) |
Python decorators example added (#2408)
Diffstat (limited to 'python.html.markdown')
-rw-r--r-- | python.html.markdown | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/python.html.markdown b/python.html.markdown index bd6090c7..f8936e65 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -5,6 +5,7 @@ contributors: - ["Amin Bandali", "http://aminbandali.com"] - ["Andre Polykanine", "https://github.com/Oire"] - ["evuez", "http://github.com/evuez"] + - ["asyne", "https://github.com/justblah"] - ["habi", "http://github.com/habi"] filename: learnpython.py --- @@ -751,6 +752,24 @@ gen_to_list = list(values) print(gen_to_list) # => [-1, -2, -3, -4, -5] # Decorators +# A decorator is a higher order function, which accepts and returns a function. +# Simple usage example – add_apples decorator will add 'Apple' element into +# fruits list returned by get_fruits target function. +def add_apples(func): + def get_fruits(): + fruits = func() + fruits.append('Apple') + return fruits + return get_fruits + +@add_apples +def get_fruits(): + return ['Banana', 'Mango', 'Orange'] + +# Prints out the list of fruits with 'Apple' element in it: +# Banana, Mango, Orange, Apple +print ', '.join(get_fruits()) + # in this example beg wraps say # Beg will call say. If say_please is True then it will change the returned # message |