Decorators:

Decorators can be hought of as functions which modify the functionality of another function. They help to make your code shorter and more 'pythonic'.


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

In [5]:
func()


Out[5]:
1

In [6]:
s = 'this is a global variable'

def func():
    print locals()

In [7]:
print globals()['s']


this is a global variable

In [8]:
func()


{}

In [9]:
def hello(name = 'rustom'):
    return 'Hello ' + name

In [10]:
hello()


Out[10]:
'Hello rustom'

In [11]:
greet = hello

In [12]:
greet


Out[12]:
<function __main__.hello>

In [13]:
greet()


Out[13]:
'Hello rustom'

In [14]:
# del hello

In [15]:
greet


Out[15]:
<function __main__.hello>

In [16]:
greet()


Out[16]:
'Hello rustom'

In [17]:
hello()


Out[17]:
'Hello rustom'

In [18]:
greet2 = hello()

In [19]:
greet2


Out[19]:
'Hello rustom'

In [20]:
greet2()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-b4134b9ba083> in <module>()
----> 1 greet2()

TypeError: 'str' object is not callable

In [21]:
del hello

In [22]:
greet2


Out[22]:
'Hello rustom'

In [23]:
greet


Out[23]:
<function __main__.hello>

In [24]:
greet()


Out[24]:
'Hello rustom'

function within function


In [25]:
def welcome(name = 'Prithvi'):
    print 'inside welcome()'
    
    def namaste():
        return '\t inside the namaste()'
    
    def hariom():
        return '\t inside hariom()'
    
    
    if name == 'Prithvi':
        return namaste
    else:
        return hariom

In [26]:
x = welcome('potter')


inside welcome()

In [27]:
print x()


	 inside hariom()

Passing functions as arguments


In [28]:
def hello():
    return 'Namaste Prithvi!!'

In [29]:
def other(func):
    print 'other code goes here'
    print func()

In [30]:
other(hello)


other code goes here
Namaste Prithvi!!

In [31]:
def new_decorator(func):
    def wrap_func():
        print 'code here....before executing the func'
        func()
        print 'code here....after executing the func'
    
    return wrap_func

In [32]:
def func_needs_decorator():
    print 'This function needs a decorator!!'

In [33]:
func_needs_decorator()


This function needs a decorator!!

In [34]:
m_func_needs_decorator = new_decorator(func_needs_decorator)

In [35]:
m_func_needs_decorator


Out[35]:
<function __main__.wrap_func>

In [36]:
m_func_needs_decorator()


code here....before executing the func
This function needs a decorator!!
code here....after executing the func

In [37]:
@new_decorator
def another_funcNeedsDecorator():
    print 'Another function needing decorator'

In [38]:
another_funcNeedsDecorator


Out[38]:
<function __main__.wrap_func>

In [39]:
another_funcNeedsDecorator()


code here....before executing the func
Another function needing decorator
code here....after executing the func

In [44]:
repr(x)


Out[44]:
'<function hariom at 0x7f9f182c96e0>'

In [45]:
str(x)


Out[45]:
'<function hariom at 0x7f9f182c96e0>'

In [ ]:


In [ ]:


In [ ]: