From b4ca1f76e2d5b955b011ea8d9b38212634fb1777 Mon Sep 17 00:00:00 2001 From: Evan Date: Thu, 9 Feb 2017 17:57:30 +0200 Subject: [PATCH] Python decorators example added (#2408) --- python.html.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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