In [1]:
def func():
    return 1

In [2]:
return_one = func

In [3]:
return_one


Out[3]:
<function __main__.func>

In [4]:
return_one()


Out[4]:
1

In [9]:
def new_decorator(func):
    print("This is a new func")
    func()

@new_decorator
def going_to_return_one():
    print "Is this a decorator"


This is a new func
Is this a decorator

In [15]:
new_decorator = going_to_return_one

In [23]:
new_decorator(lambda x, y: x**y)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-b8b79f0b294f> in <module>()
----> 1 new_decorator(lambda x, y: x**y)

TypeError: 'NoneType' object is not callable

In [25]:
def hello(name="Jose"):
    print("Hello %s" % name)

In [26]:
hello()


Hello Jose

In [28]:
greet = hello

In [29]:
greet()


Hello Jose

In [30]:
globals()


Out[30]:
{'In': ['',
  u'def func():\n    return 1',
  u'return_one = func',
  u'return_one',
  u'return_one()',
  u'@new_decorator\ndef going_to_return_one',
  u'@new_decorator\ndef going_to_return_one:',
  u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
  u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
  u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
  u'going_to_return_one',
  u'going_to_return_one()',
  u'new_decorator',
  u'new_decorator()',
  u'new_decorator = going_to_return_one()',
  u'new_decorator = going_to_return_one',
  u'new_decorator',
  u'new_decorator()',
  u'new_decorator(lambda: x, y: x**y)',
  u'new_decorator(lambda: x, y, x**y)',
  u'new_decorator(lambda: x, x**y)',
  u'new_decorator(lambda: x: x**y)',
  u'new_decorator(lambda x: x**y)',
  u'new_decorator(lambda x, y: x**y)',
  u'def hello(name="Jose"):',
  u'def hello(name="Jose"):\n    print("Hello %s" % name)',
  u'hello()',
  u'greet = hello"',
  u'greet = hello',
  u'greet()',
  u'globals()'],
 'Out': {3: <function __main__.func>,
  4: 1,
  12: <function __main__.new_decorator>},
 '_': <function __main__.new_decorator>,
 '_12': <function __main__.new_decorator>,
 '_3': <function __main__.func>,
 '_4': 1,
 '__': 1,
 '___': <function __main__.func>,
 '__builtin__': <module '__builtin__' (built-in)>,
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': 'Automatically created module for IPython interactive environment',
 '__name__': '__main__',
 '_dh': [u'/Users/louistur/Developer/Python/PythonBootcamp'],
 '_i': u'greet()',
 '_i1': u'def func():\n    return 1',
 '_i10': u'going_to_return_one',
 '_i11': u'going_to_return_one()',
 '_i12': u'new_decorator',
 '_i13': u'new_decorator()',
 '_i14': u'new_decorator = going_to_return_one()',
 '_i15': u'new_decorator = going_to_return_one',
 '_i16': u'new_decorator',
 '_i17': u'new_decorator()',
 '_i18': u'new_decorator(lambda: x, y: x**y)',
 '_i19': u'new_decorator(lambda: x, y, x**y)',
 '_i2': u'return_one = func',
 '_i20': u'new_decorator(lambda: x, x**y)',
 '_i21': u'new_decorator(lambda: x: x**y)',
 '_i22': u'new_decorator(lambda x: x**y)',
 '_i23': u'new_decorator(lambda x, y: x**y)',
 '_i24': u'def hello(name="Jose"):',
 '_i25': u'def hello(name="Jose"):\n    print("Hello %s" % name)',
 '_i26': u'hello()',
 '_i27': u'greet = hello"',
 '_i28': u'greet = hello',
 '_i29': u'greet()',
 '_i3': u'return_one',
 '_i30': u'globals()',
 '_i4': u'return_one()',
 '_i5': u'@new_decorator\ndef going_to_return_one',
 '_i6': u'@new_decorator\ndef going_to_return_one:',
 '_i7': u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
 '_i8': u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
 '_i9': u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
 '_ih': ['',
  u'def func():\n    return 1',
  u'return_one = func',
  u'return_one',
  u'return_one()',
  u'@new_decorator\ndef going_to_return_one',
  u'@new_decorator\ndef going_to_return_one:',
  u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
  u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
  u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
  u'going_to_return_one',
  u'going_to_return_one()',
  u'new_decorator',
  u'new_decorator()',
  u'new_decorator = going_to_return_one()',
  u'new_decorator = going_to_return_one',
  u'new_decorator',
  u'new_decorator()',
  u'new_decorator(lambda: x, y: x**y)',
  u'new_decorator(lambda: x, y, x**y)',
  u'new_decorator(lambda: x, x**y)',
  u'new_decorator(lambda: x: x**y)',
  u'new_decorator(lambda x: x**y)',
  u'new_decorator(lambda x, y: x**y)',
  u'def hello(name="Jose"):',
  u'def hello(name="Jose"):\n    print("Hello %s" % name)',
  u'hello()',
  u'greet = hello"',
  u'greet = hello',
  u'greet()',
  u'globals()'],
 '_ii': u'greet = hello',
 '_iii': u'greet = hello"',
 '_oh': {3: <function __main__.func>,
  4: 1,
  12: <function __main__.new_decorator>},
 '_sh': <module 'IPython.core.shadowns' from '/Users/louistur/anaconda/lib/python2.7/site-packages/IPython/core/shadowns.pyc'>,
 'exit': <IPython.core.autocall.ZMQExitAutocall at 0x10338f3d0>,
 'func': <function __main__.func>,
 'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x103252ed0>>,
 'going_to_return_one': None,
 'greet': <function __main__.hello>,
 'hello': <function __main__.hello>,
 'new_decorator': None,
 'quit': <IPython.core.autocall.ZMQExitAutocall at 0x10338f3d0>,
 'return_one': <function __main__.func>}

In [31]:
globals()['Out']


Out[31]:
{3: <function __main__.func>,
 4: 1,
 12: <function __main__.new_decorator>,
 30: {'In': ['',
   u'def func():\n    return 1',
   u'return_one = func',
   u'return_one',
   u'return_one()',
   u'@new_decorator\ndef going_to_return_one',
   u'@new_decorator\ndef going_to_return_one:',
   u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
   u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
   u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
   u'going_to_return_one',
   u'going_to_return_one()',
   u'new_decorator',
   u'new_decorator()',
   u'new_decorator = going_to_return_one()',
   u'new_decorator = going_to_return_one',
   u'new_decorator',
   u'new_decorator()',
   u'new_decorator(lambda: x, y: x**y)',
   u'new_decorator(lambda: x, y, x**y)',
   u'new_decorator(lambda: x, x**y)',
   u'new_decorator(lambda: x: x**y)',
   u'new_decorator(lambda x: x**y)',
   u'new_decorator(lambda x, y: x**y)',
   u'def hello(name="Jose"):',
   u'def hello(name="Jose"):\n    print("Hello %s" % name)',
   u'hello()',
   u'greet = hello"',
   u'greet = hello',
   u'greet()',
   u'globals()',
   u"globals()['Out']"],
  'Out': {...},
  '_': {...},
  '_12': <function __main__.new_decorator>,
  '_3': <function __main__.func>,
  '_30': {...},
  '_4': 1,
  '__': <function __main__.new_decorator>,
  '___': 1,
  '__builtin__': <module '__builtin__' (built-in)>,
  '__builtins__': <module '__builtin__' (built-in)>,
  '__doc__': 'Automatically created module for IPython interactive environment',
  '__name__': '__main__',
  '_dh': [u'/Users/louistur/Developer/Python/PythonBootcamp'],
  '_i': u'globals()',
  '_i1': u'def func():\n    return 1',
  '_i10': u'going_to_return_one',
  '_i11': u'going_to_return_one()',
  '_i12': u'new_decorator',
  '_i13': u'new_decorator()',
  '_i14': u'new_decorator = going_to_return_one()',
  '_i15': u'new_decorator = going_to_return_one',
  '_i16': u'new_decorator',
  '_i17': u'new_decorator()',
  '_i18': u'new_decorator(lambda: x, y: x**y)',
  '_i19': u'new_decorator(lambda: x, y, x**y)',
  '_i2': u'return_one = func',
  '_i20': u'new_decorator(lambda: x, x**y)',
  '_i21': u'new_decorator(lambda: x: x**y)',
  '_i22': u'new_decorator(lambda x: x**y)',
  '_i23': u'new_decorator(lambda x, y: x**y)',
  '_i24': u'def hello(name="Jose"):',
  '_i25': u'def hello(name="Jose"):\n    print("Hello %s" % name)',
  '_i26': u'hello()',
  '_i27': u'greet = hello"',
  '_i28': u'greet = hello',
  '_i29': u'greet()',
  '_i3': u'return_one',
  '_i30': u'globals()',
  '_i31': u"globals()['Out']",
  '_i4': u'return_one()',
  '_i5': u'@new_decorator\ndef going_to_return_one',
  '_i6': u'@new_decorator\ndef going_to_return_one:',
  '_i7': u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
  '_i8': u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
  '_i9': u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
  '_ih': ['',
   u'def func():\n    return 1',
   u'return_one = func',
   u'return_one',
   u'return_one()',
   u'@new_decorator\ndef going_to_return_one',
   u'@new_decorator\ndef going_to_return_one:',
   u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
   u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
   u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
   u'going_to_return_one',
   u'going_to_return_one()',
   u'new_decorator',
   u'new_decorator()',
   u'new_decorator = going_to_return_one()',
   u'new_decorator = going_to_return_one',
   u'new_decorator',
   u'new_decorator()',
   u'new_decorator(lambda: x, y: x**y)',
   u'new_decorator(lambda: x, y, x**y)',
   u'new_decorator(lambda: x, x**y)',
   u'new_decorator(lambda: x: x**y)',
   u'new_decorator(lambda x: x**y)',
   u'new_decorator(lambda x, y: x**y)',
   u'def hello(name="Jose"):',
   u'def hello(name="Jose"):\n    print("Hello %s" % name)',
   u'hello()',
   u'greet = hello"',
   u'greet = hello',
   u'greet()',
   u'globals()',
   u"globals()['Out']"],
  '_ii': u'greet()',
  '_iii': u'greet = hello',
  '_oh': {...},
  '_sh': <module 'IPython.core.shadowns' from '/Users/louistur/anaconda/lib/python2.7/site-packages/IPython/core/shadowns.pyc'>,
  'exit': <IPython.core.autocall.ZMQExitAutocall at 0x10338f3d0>,
  'func': <function __main__.func>,
  'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x103252ed0>>,
  'going_to_return_one': None,
  'greet': <function __main__.hello>,
  'hello': <function __main__.hello>,
  'new_decorator': None,
  'quit': <IPython.core.autocall.ZMQExitAutocall at 0x10338f3d0>,
  'return_one': <function __main__.func>}}

In [32]:
locals()


Out[32]:
{'In': ['',
  u'def func():\n    return 1',
  u'return_one = func',
  u'return_one',
  u'return_one()',
  u'@new_decorator\ndef going_to_return_one',
  u'@new_decorator\ndef going_to_return_one:',
  u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
  u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
  u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
  u'going_to_return_one',
  u'going_to_return_one()',
  u'new_decorator',
  u'new_decorator()',
  u'new_decorator = going_to_return_one()',
  u'new_decorator = going_to_return_one',
  u'new_decorator',
  u'new_decorator()',
  u'new_decorator(lambda: x, y: x**y)',
  u'new_decorator(lambda: x, y, x**y)',
  u'new_decorator(lambda: x, x**y)',
  u'new_decorator(lambda: x: x**y)',
  u'new_decorator(lambda x: x**y)',
  u'new_decorator(lambda x, y: x**y)',
  u'def hello(name="Jose"):',
  u'def hello(name="Jose"):\n    print("Hello %s" % name)',
  u'hello()',
  u'greet = hello"',
  u'greet = hello',
  u'greet()',
  u'globals()',
  u"globals()['Out']",
  u'locals()'],
 'Out': {3: <function __main__.func>,
  4: 1,
  12: <function __main__.new_decorator>,
  30: {...}},
 '_': {...},
 '_12': <function __main__.new_decorator>,
 '_3': <function __main__.func>,
 '_30': {...},
 '_4': 1,
 '__': <function __main__.new_decorator>,
 '___': 1,
 '__builtin__': <module '__builtin__' (built-in)>,
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': 'Automatically created module for IPython interactive environment',
 '__name__': '__main__',
 '_dh': [u'/Users/louistur/Developer/Python/PythonBootcamp'],
 '_i': u"globals()['Out']",
 '_i1': u'def func():\n    return 1',
 '_i10': u'going_to_return_one',
 '_i11': u'going_to_return_one()',
 '_i12': u'new_decorator',
 '_i13': u'new_decorator()',
 '_i14': u'new_decorator = going_to_return_one()',
 '_i15': u'new_decorator = going_to_return_one',
 '_i16': u'new_decorator',
 '_i17': u'new_decorator()',
 '_i18': u'new_decorator(lambda: x, y: x**y)',
 '_i19': u'new_decorator(lambda: x, y, x**y)',
 '_i2': u'return_one = func',
 '_i20': u'new_decorator(lambda: x, x**y)',
 '_i21': u'new_decorator(lambda: x: x**y)',
 '_i22': u'new_decorator(lambda x: x**y)',
 '_i23': u'new_decorator(lambda x, y: x**y)',
 '_i24': u'def hello(name="Jose"):',
 '_i25': u'def hello(name="Jose"):\n    print("Hello %s" % name)',
 '_i26': u'hello()',
 '_i27': u'greet = hello"',
 '_i28': u'greet = hello',
 '_i29': u'greet()',
 '_i3': u'return_one',
 '_i30': u'globals()',
 '_i31': u"globals()['Out']",
 '_i32': u'locals()',
 '_i4': u'return_one()',
 '_i5': u'@new_decorator\ndef going_to_return_one',
 '_i6': u'@new_decorator\ndef going_to_return_one:',
 '_i7': u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
 '_i8': u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
 '_i9': u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
 '_ih': ['',
  u'def func():\n    return 1',
  u'return_one = func',
  u'return_one',
  u'return_one()',
  u'@new_decorator\ndef going_to_return_one',
  u'@new_decorator\ndef going_to_return_one:',
  u'@new_decorator\ndef going_to_return_one:\n    print "Is this a decorator"',
  u'@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"',
  u'def new_decorator(func):\n    print("This is a new func")\n    func()\n\n@new_decorator\ndef going_to_return_one():\n    print "Is this a decorator"\n    ',
  u'going_to_return_one',
  u'going_to_return_one()',
  u'new_decorator',
  u'new_decorator()',
  u'new_decorator = going_to_return_one()',
  u'new_decorator = going_to_return_one',
  u'new_decorator',
  u'new_decorator()',
  u'new_decorator(lambda: x, y: x**y)',
  u'new_decorator(lambda: x, y, x**y)',
  u'new_decorator(lambda: x, x**y)',
  u'new_decorator(lambda: x: x**y)',
  u'new_decorator(lambda x: x**y)',
  u'new_decorator(lambda x, y: x**y)',
  u'def hello(name="Jose"):',
  u'def hello(name="Jose"):\n    print("Hello %s" % name)',
  u'hello()',
  u'greet = hello"',
  u'greet = hello',
  u'greet()',
  u'globals()',
  u"globals()['Out']",
  u'locals()'],
 '_ii': u'globals()',
 '_iii': u'greet()',
 '_oh': {3: <function __main__.func>,
  4: 1,
  12: <function __main__.new_decorator>,
  30: {...}},
 '_sh': <module 'IPython.core.shadowns' from '/Users/louistur/anaconda/lib/python2.7/site-packages/IPython/core/shadowns.pyc'>,
 'exit': <IPython.core.autocall.ZMQExitAutocall at 0x10338f3d0>,
 'func': <function __main__.func>,
 'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x103252ed0>>,
 'going_to_return_one': None,
 'greet': <function __main__.hello>,
 'hello': <function __main__.hello>,
 'new_decorator': None,
 'quit': <IPython.core.autocall.ZMQExitAutocall at 0x10338f3d0>,
 'return_one': <function __main__.func>}

In [34]:
def hello_2(name="louis"):
    # local functions
    print 'The hello() function has been executed'
    
    def greet():
        return '\t this is inside of the greet() function'
    
    def welcome():
        return '\t this is inside of the welcome() function'
    
    print greet()
    print welcome()
    print 'Now we are back inside the hellow_2 function'

In [35]:
hello_2()


The hello() function has been executed
	 this is inside of the greet() function
	 this is inside of the welcome() function
Now we are back inside the hellow_2 function

In [44]:
# returning functions
def hello_3(name="louis"):
    def greet():
        return '\t this is inside of the greet() function'
    
    def welcome():
        return '\t this is inside of the welcome() function'
    
    if name == "louis":
        return greet
    else:
        return welcome

In [48]:
x = hello_3()

In [50]:
print x()


	 this is inside of the greet() function

In [53]:
# functions as arguments
def hello_4():
    return 'Hi Louis'

def other(func):
    print "Other code goes here"
    print func()
    
other(hello_4)


Other code goes here
Hi Louis

In [55]:
def new_decorator(func):
    def wrap_func():
        print 'Code here, before executing func'
        func()
        print 'Code here will execute after the func'
    
    # note: returning the function itself, not the results of the func
    return wrap_func

def func_needs_decorator():
    print 'This function needs a decorator'

In [56]:
func_needs_decorator()


This function needs a decorator

In [57]:
func_needs_decorator = new_decorator(func_needs_decorator)

In [58]:
func_needs_decorator()


Code here, before executing func
This function needs a decorator
Code here will execute after the func

In [110]:
def cat_decoy(func):
    def wrap_func():
        print('In decoy')
        func()
    return wrap_func

def catnip1():
    print('In catnip')

In [111]:
catnip1 = cat_decoy(catnip1)

In [114]:
@cat_decoy
def catnip():
    print 'In cat nip 2'
    
@cat_decoy
def catnip_original():
    print 'Original'

In [113]:
catnip()


In decoy
In cat nip 2

In [115]:
catnip_original()


In decoy
Original

In [ ]: