diff options
author | charalambosp <papaloizouc@gmail.com> | 2014-02-16 18:36:09 +0000 |
---|---|---|
committer | charalambosp <papaloizouc@gmail.com> | 2014-02-16 18:36:09 +0000 |
commit | 550b9c9f2ebf8ead67eb723d3b4256064e284b82 (patch) | |
tree | 7c3a6d0e2434c6a848ad246febbbab4cb7080267 /python.html.markdown | |
parent | b8e4a27551f0d3cec1724113ed45b4f44bbacee0 (diff) |
generators & decorators
Diffstat (limited to 'python.html.markdown')
-rw-r--r-- | python.html.markdown | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/python.html.markdown b/python.html.markdown index 941ba9f4..fe0ddc8c 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -473,6 +473,54 @@ import math dir(math) +#################################################### +## 6. Advanced +#################################################### + +# Generators help you make lazy code +def double_numbers(iterable): + for i in iterable: + yield i + i + +# generator creates the value on the fly +# instead of generating and returning all values at once it creates one in each iteration +# this means values bigger than 15 wont be processed in double_numbers +# note range is a generator too, creating a list 1-900000000 would take lot of time to be made +_range = range(1, 900000000) +# will double all numbers until a result >=30 found +for i in double_numbers(_range): + print(i) + if i >= 30: + break + + +# Decorators +# in this example beg wraps say +# Beg will call say. If say_please is True then it will change the returned message +from functools import wraps + + +def beg(_say): + @wraps(_say) + def wrapper(*args, **kwargs): + msg, say_please = _say(*args, **kwargs) + if say_please: + return "{} {}".format(msg, "Please! I am poor :(") + return msg + + return wrapper + + +@beg +def say(say_please=False): + msg = "Can you buy me a beer?" + return msg, say_please + + +print(say()) # Can you buy me a beer? +print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :( + + ``` ## Ready For More? |