In [1]:
def shout(word="yes"):
return word.capitalize()+"!"
print(shout())
scream = shout
print(scream())
del shout
try:
print(shout())
except NameError, e:
print e
print(scream())
In [2]:
def talk():
def whisper(word="yes"):
return word.lower()+"...";
print whisper()
In [3]:
talk()
try:
print whisper()
except NameError, e:
print e
In [4]:
def getTalk(type="shout"):
def shout(word="yes"):
return word.capitalize()+"!"
def whisper(word="yes") :
return word.lower()+"...";
if type == "shout":
return shout
else:
return whisper
In [5]:
talk = getTalk()
print talk
print talk()
print getTalk("whisper")()
In [ ]:
def doSomethingBefore(func):
print "Doing something before... the call"
print func()
In [ ]:
doSomethingBefore(scream)
In [ ]:
def my_shiny_new_decorator(a_function_to_decorate):
def the_wrapper_around_the_original_function():
print "Code before the call"
a_function_to_decorate()
print "Call after the call"
return the_wrapper_around_the_original_function
In [ ]:
def a_stand_alone_function():
print "Single function.... Lonely... so lonely"
In [ ]:
a_stand_alone_function()
a_stand_alone_function_decorated = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function_decorated()
In [ ]:
a_stand_alone_function = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function()
In [ ]:
@my_shiny_new_decorator
def another_stand_alone_function():
print "Just live me alone..."
In [ ]:
another_stand_alone_function()
In [ ]:
another_stand_alone_function = my_shiny_new_decorator(another_stand_alone_function)
In [ ]:
In [ ]: